使用 jsoncpp 解析 JSON 字符串
Parsing JSON string with jsoncpp
我正在尝试解析用 PHP 编码并通过 TCP 发送到 C++ 客户端的 JSON 字符串。
我的JSON字符串是这样的:
{"1":{"name":"MIKE","surname":"TAYLOR"},"2":{"name":"TOM","surname":"JERRY"}}
在 C++ 客户端上,我使用的是 jsoncpp 库:
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;
}
}
问题是我没有得到任何输出,甚至没有关于解析的错误(如果有的话)。
你能帮我理解我做错了什么吗?
你的问题是:没有root["name"]。您的文档应该是这样的:
{ "people": [{"id": 1, "name":"MIKE","surname":"TAYLOR"}, {"id": 2, "name":"TOM","surname":"JERRY"} ]}
你的代码是这样的:
void decode()
{
string text ="{ \"people\": [{\"id\": 1, \"name\":\"MIKE\",\"surname\":\"TAYLOR\"}, {\"id\": 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["people"];
for ( int index = 0; index < mynames.size(); ++index )
{
cout << mynames[index] << endl;
}
}
如果您想保持数据不变:
void decode()
{
//string text ="{ \"people\": [{\"id\": 1, \"name\":\"MIKE\",\"surname\":\"TAYLOR\"}, {\"id\": 2, \"name\":\"TOM\",\"surname\":\"JERRY\"} ]}";
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;
}
for( Json::Value::const_iterator outer = root.begin() ; outer != root.end() ; outer++ )
{
for( Json::Value::const_iterator inner = (*outer).begin() ; inner!= (*outer).end() ; inner++ )
{
cout << inner.key() << ": " << *inner << endl;
}
}
}
直接遍历根对象,使用迭代器(不要把它当作数组。
如果Json::Reader不起作用,尝试Json::CharReader 而不是:
void decode()
{
string text ="{\"1\":{\"name\":\"MIKE\",\"surname\":\"TAYLOR\"},\"2\":{\"name\":\"TOM\",\"surname\":\"JERRY\"}}";
Json::CharReaderBuilder builder;
Json::CharReader * reader = builder.newCharReader();
Json::Value root;
string errors;
bool parsingSuccessful = reader->parse(text.c_str(), text.c_str() + text.size(), &root, &errors);
delete reader;
if ( !parsingSuccessful )
{
cout << text << endl;
cout << errors << endl;
}
for( Json::Value::const_iterator outer = root.begin() ; outer != root.end() ; outer++ )
{
for( Json::Value::const_iterator inner = (*outer).begin() ; inner!= (*outer).end() ; inner++ )
{
cout << inner.key() << ": " << *inner << endl;
}
}
}
您还可以从字符串流中读取:
std::stringstream sstr(stringJson);
Json::Value json;
sstr >> json;
我正在尝试解析用 PHP 编码并通过 TCP 发送到 C++ 客户端的 JSON 字符串。
我的JSON字符串是这样的:
{"1":{"name":"MIKE","surname":"TAYLOR"},"2":{"name":"TOM","surname":"JERRY"}}
在 C++ 客户端上,我使用的是 jsoncpp 库:
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;
}
}
问题是我没有得到任何输出,甚至没有关于解析的错误(如果有的话)。 你能帮我理解我做错了什么吗?
你的问题是:没有root["name"]。您的文档应该是这样的:
{ "people": [{"id": 1, "name":"MIKE","surname":"TAYLOR"}, {"id": 2, "name":"TOM","surname":"JERRY"} ]}
你的代码是这样的:
void decode()
{
string text ="{ \"people\": [{\"id\": 1, \"name\":\"MIKE\",\"surname\":\"TAYLOR\"}, {\"id\": 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["people"];
for ( int index = 0; index < mynames.size(); ++index )
{
cout << mynames[index] << endl;
}
}
如果您想保持数据不变:
void decode()
{
//string text ="{ \"people\": [{\"id\": 1, \"name\":\"MIKE\",\"surname\":\"TAYLOR\"}, {\"id\": 2, \"name\":\"TOM\",\"surname\":\"JERRY\"} ]}";
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;
}
for( Json::Value::const_iterator outer = root.begin() ; outer != root.end() ; outer++ )
{
for( Json::Value::const_iterator inner = (*outer).begin() ; inner!= (*outer).end() ; inner++ )
{
cout << inner.key() << ": " << *inner << endl;
}
}
}
直接遍历根对象,使用迭代器(不要把它当作数组。
如果Json::Reader不起作用,尝试Json::CharReader 而不是:
void decode()
{
string text ="{\"1\":{\"name\":\"MIKE\",\"surname\":\"TAYLOR\"},\"2\":{\"name\":\"TOM\",\"surname\":\"JERRY\"}}";
Json::CharReaderBuilder builder;
Json::CharReader * reader = builder.newCharReader();
Json::Value root;
string errors;
bool parsingSuccessful = reader->parse(text.c_str(), text.c_str() + text.size(), &root, &errors);
delete reader;
if ( !parsingSuccessful )
{
cout << text << endl;
cout << errors << endl;
}
for( Json::Value::const_iterator outer = root.begin() ; outer != root.end() ; outer++ )
{
for( Json::Value::const_iterator inner = (*outer).begin() ; inner!= (*outer).end() ; inner++ )
{
cout << inner.key() << ": " << *inner << endl;
}
}
}
您还可以从字符串流中读取:
std::stringstream sstr(stringJson);
Json::Value json;
sstr >> json;