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;)
Wow, to był najlepszy szybkie podkład na C++ i json widziałem. niesamowite –
Dzięki, oszczędzasz czas. – nenchev
Z nieprzetworzonym łańcuchem (C++ 11) możesz użyć 'strJson = R" ({"mykey": "myvalue"}) ";'. – Jarod42