0

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?

2 Answers 2

4
  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
Sign up to request clarification or add additional context in comments.

1 Comment

So do you have to re-read the root object at the base of the json array ? I don't understand.
0

For arrays with only 1 object, use 0.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.