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?
your root contains 2 elements "return" and "entries" so or
root["return"]
orroot["entries"]
Then - array contains a list of members - so even if it only one entry - you still have to get it.
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
Output