I'm trying to parse a JSON string encoded with PHP and sent over TCP to a C++ client.
My JSON strings are like this:
{"1":{"name":"MIKE","surname":"TAYLOR"},"2":{"name":"TOM","surname":"JERRY"}}
On the C++ client I'm using the jsoncpp libraries:
void decode()
{
string text = {"1":{"name":"MIKE","surname":"TAYLOR"},"2":{"name":"TOM","surname":"JERRY"}};
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse( text, root );
if ( !parsingSuccessful )
{
cout << "Error parsing the string" << endl;
}
const Json::Value mynames = root["name"];
for ( int index = 0; index < mynames.size(); ++index )
{
cout << mynames[index] << endl;
}
}
The problem is that I'm not getting anything as output, not even the error about the parsing(if any). Could you possibly help me to understand what I'm doing wrong ?
Example - Parsing JSONUse the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.
JSON (JavaScript Object Notation) is a straightforward data exchange format to interchange the server's data, and it is a better alternative for XML. This is because JSON is a lightweight and structured language.
Simple JSON Parser in C++ using JsonCpp library We can use Json::Reader and Json::Writer for reading and writing in JSON files. Json::reader will read the JSON file and will return the value as Json::Value . Parsing the JSON is very simple by using parse() .
You can also read from a stringstream:
std::stringstream sstr(stringJson);
Json::Value json;
sstr >> json;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With