用jsoncpp解析JSON字符串

用jsoncpp解析JSON字符串

本文介绍了用jsoncpp解析JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一个用PHP编码并通过TCP发送到C ++客户端的JSON字符串.

I'm trying to parse a JSON string encoded with PHP and sent over TCP to a C++ client.

我的JSON字符串如下:

My JSON strings are like this:

{"1":{"name":"MIKE","surname":"TAYLOR"},"2":{"name":"TOM","surname":"JERRY"}}

在C ++客户端上,我正在使用jsoncpp库:

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 ?

推荐答案

您的问题是:没有 root ["name"] .您的文档应如下所示:

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"} ]}

您的代码如下:

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.

如果 Json :: Reader 不起作用,请尝试使用 Json :: CharReader :

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;
    }
  }
}

这篇关于用jsoncpp解析JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:19