2015-01-12 10 views
6

ho questo oggetto JSON:jsoncpp. trovare l'oggetto nella matrice facendo corrispondere valore

{"books":[ 
    { 
     "author" : "Petr", 
     "book_name" : "Test1", 
     "pages" : 200, 
     "year" : 2002 
    }, 
    { 
     "author" : "Petr", 
     "book_name" : "Test2", 
     "pages" : 0, 
     "year" : 0 
    }, 
    { 
     "author" : "STO", 
     "book_name" : "Rocks", 
     "pages" : 100, 
     "year" : 2002 
    } 
    ] 
} 

Ad esempio, ho bisogno di trovare un libro (s) che author chiave è uguale a Petr. Come posso fare questo? In questo momento ho questo codice:

Json::Value findBook(){ 
    Json::Value root = getRoot(); 

    cout<<root["books"].toStyledString()<<endl; //Prints JSON array of books mentioned above 

    string searchKey; 
    cout<<"Enter search key: "; 
    cin>>searchKey; 

    string searchValue; 
    cout<<"Enter search value: "; 
    cin>>searchValue; 

    Json::Value foundBooks = root["books"]???; // How can I get here a list of books where searchKey is equal to searchValue? 
} 

Grazie in anticipo.

+1

Continui a dire "non funziona". Si prega di prendere l'abitudine di presentare descrizioni dei problemi concreti, insieme a _evidence_. "Non funziona" è fondamentalmente inutile. –

+0

@LightnessRacesinOrbit Ciao. Mi dispiace davvero per quello. Ho contrassegnato la soluzione di Barry come corretta ed è vero. Nel mio caso il problema era con IDE CLion di Jetbrains, che è attualmente disponibile solo come versione EAP. C'era solo qualche bug con IDE, quando dopo aver compilato un progetto ha avviato la vecchia app. –

risposta

7

Qualcosa del genere dovrebbe fare:

std::vector<Json::Value> booksByPeter(const Json::Value& root) { 
    std::vector<Json::Value> res; 
    for (const Json::Value& book : root["books"]) // iterate over "books" 
    { 
     if (book["author"].asString() == "Petr") // if by "Petr" 
     { 
      res.push_back(book);     // take a copy 
     } 
    } 
    return res;         // and return 
} 

Se non C++ 11, sarà invece hanno a che fare:

const Json::Value& books = root["books"]; 
for (Json::ValueConstIterator it = books.begin(); it != books.end(); ++it) 
{ 
    const Json::Value& book = *it; 
    // rest as before 
} 
+0

grazie. Ho solo pensato che ci fosse un elegante jsoncpp costruito in funzione per queste esigenze. –

+2

@PeterShipilo Sarei molto sorpreso se ci fosse - solo facendo il modo semplice sembra molto elegante e semplice per me. – Barry

3

È possibile iterare su array JSON come contenitori STL:

std::vector<Json::Value> SearchInArray(const Json::Value &json, const std::string &key, const std::string &value) 
{ 
    assert(json.isArray()); 
    std::vector<Json::Value> results; 
    for (size_t i = 0; i != json.size(); i++) 
     if (json[i][key].asString() == value) 
      results.push_back(json[i]); 
    return results; 
} 

Utilizzare in questo modo:

std::vector<Json::Value> results = SearchInArray(json["books"], "author", "Petr"); 
Problemi correlati