jsoncpp去掉多余字符,用jsoncpp解析JSON字符串

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 ?

解决方案

Your problem is: there is no root["name"]. Your document should be like this:

{ "people": [{"id": 1, "name":"MIKE","surname":"TAYLOR"}, {"id": 2, "name":"TOM","surname":"JERRY"} ]}

And your code like this:

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;

}

}

If you want to keep your data as is:

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;

}

}

}

Traverse the root object directly, using iterators (don't treat it as it was an array.

If Json::Reader doesn't work, try Json::CharReader instead:

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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值