简体   繁体   中英

Reading Array of the json in JsonCpp

I tried to write a simple JSON reader for my program then I use JsonCpp. I have this JSON from my web server:

{
  "return":
  {
    "status":200,
    "message":"Accepted"
  },
  "entries":
  [
   {
     "messageid":185002992,
     "message":"CplusItsGood",
     "status":1,
     "statustext":"test",
     "sender":"1234567",
     "receptor":"123456789",
     "date":1234,
     "cost":140
   }
  ]
}

And this is my C++ code:

    Json::Reader reader;
    Json::Value root;

    reader.parse(jsonContext, root, false);

    const Json::Value entriesArray = root["return"]["entries"];

    int A = entriesArray["sender"].asInt();

    cout << A;

It's print only 0 , I can't read the sender or any other element of the entries array. I want get the value of the cost or sender for example.

How can I do that?

  1. your root contains 2 elements "return" and "entries" so or root["return"] or root["entries"]

  2. Then - array contains a list of members - so even if it only one entry - you still have to get it.

  3. if value is quoted - it is string - you cannot use getInt on it. For example getInt is applicable to "status" not "sender"

Here is the whole sample

#include <iostream>
#include <string>
#include <json/json.h>
int main()
{

    std::string s = R"({
        "return":
        {
        "status":200,
        "message":"Accepted"
        },
        "entries":
        [
        {
        "messageid":185002992,
        "message":"CplusItsGood",
        "status":1,
        "statustext":"test",
        "sender":"1234567",
        "receptor":"123456789",
        "date":1234,
        "cost":140
        }
        ]
    })";


    Json::Reader reader;
    Json::Value root;

    reader.parse(s, root, false);

    auto entriesArray = root["entries"];

    auto firstelem = entriesArray[0];
    std::string sender = firstelem["sender"].asString();
    int i = std::stoi(sender);
    std::cout << "array:" << entriesArray << "\n";
    std::cout << "element:" << firstelem << "\n";
    std::cout << "value:" << sender << "\n";
    std::cout << "parsed value:" << i << "\n";
}

Output

array:[
        {
                "cost" : 140,
                "date" : 1234,
                "message" : "CplusItsGood",
                "messageid" : 185002992,
                "receptor" : "123456789",
                "sender" : "1234567",
                "status" : 1,
                "statustext" : "test"
        }
]
element:{
        "cost" : 140,
        "date" : 1234,
        "message" : "CplusItsGood",
        "messageid" : 185002992,
        "receptor" : "123456789",
        "sender" : "1234567",
        "status" : 1,
        "statustext" : "test"
}
value:1234567
parsed value:1234567

For arrays with only 1 object, use 0.

const Json::Value entriesArray = root["entries"][0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM