2015-06-29 13 views

Odpowiedz

19

Witam to jest całkiem prosta:

1 - Potrzebny jest wartość obiektu CPP JSON (JSON :: wartość) do przechowywania danych

2 - Użyj Json Reader (JSON :: Reader) czytać i analizować JSON ciąg w JSON obiektu

3 - Zrób swoje rzeczy :)

Oto prosty kod, aby te czynności:

#include <stdio.h> 
#include <jsoncpp/json/json.h> 
#include <jsoncpp/json/reader.h> 
#include <jsoncpp/json/writer.h> 
#include <jsoncpp/json/value.h> 
#include <string> 

int main(int argc, const char* argv[]) 
{ 

    std::string strJson = "{\"mykey\" : \"myvalue\"}"; // need escape the quotes 

    Json::Value root; 
    Json::Reader reader; 
    bool parsingSuccessful = reader.parse(strJson.c_str(), root);  //parse process 
    if (!parsingSuccessful) 
    { 
     std::cout << "Failed to parse" 
       << reader.getFormattedErrorMessages(); 
     return 0; 
    } 
    std::cout << root.get("mykey", "A Default Value if not exists").asString() << std::endl; 
    return 0; 
} 

Aby skompilować: g ++ -o main YourMainFile.cpp -l jsoncpp

Mam nadzieję, że to pomaga;)

+0

Wow, to był najlepszy szybkie podkład na C++ i json widziałem. niesamowite –

+0

Dzięki, oszczędzasz czas. – nenchev

+2

Z nieprzetworzonym łańcuchem (C++ 11) możesz użyć 'strJson = R" ({"mykey": "myvalue"}) ";'. – Jarod42

Powiązane problemy