Parsing JSON string with jsoncpp

前端 未结 2 1676
有刺的猬
有刺的猬 2020-12-11 04:16

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\",\"sur         


        
相关标签:
2条回答
  • 2020-12-11 05:12

    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)
  • 2020-12-11 05:13

    You can also read from a stringstream:

    std::stringstream sstr(stringJson);
    Json::Value json;
    sstr >> json;
    
    0 讨论(0)
提交回复
热议问题