0

I got a little problem. My json looks like this:

{
"data" : 
{
    "Money" : 
    {
        "testUser3" : "500",
        "testUser2" : "23",
        "testUser1" : "2435",
        "testUser" : "60"
    }
}}

And I am trying to get the value + names of all the users.. but I don't know how, because the user names can change I can't use root["data"]["Money"]["testUser"].asInt(); I hope someone can help me, thanks.

1
  • 1
    Look at this page, there's an example. Commented Apr 15, 2018 at 16:05

1 Answer 1

2

you were downvoted, but I'll help you anyway, cause I need something similar.. let's thank Brandon in JsonCpp - when having a json::Value object, how can i know it's key name? for the keys part..

#include <string>
#include <iostream>

#include "jsoncpp-master/include/json/value.h"
#include "jsoncpp-master/include/json/json.h"

using namespace std;

bool Testjson2()
{
    string s =
        "   { \"data\" :{"
        "           \"Money\" : {"
        "               \"testUser3\" : \"500\","
        "               \"testUser2\" : \"23\","
        "               \"testUser1\" : \"2435\","
        "               \"testUser\"  : \"60\""
        "           }"
        "   } }";
    Json::Value root;
    Json::Reader reader;
    string sdress = "{\"\":[" + s + "]}";
    if (!reader.parse(sdress, root))
    {
        cout << sdress << "\n" << "Parse error\n";
        return false;
    }
    const Json::Value datasections = root[""];
    Json::Value  v = datasections [0]["data"]["Money"];
    cout << "There are " << v.size() << " members\n";
    for (auto const& id : v.getMemberNames()) 
        cout << id << " has " << v[id] << std::endl;
    return true;
}
Sign up to request clarification or add additional context in comments.

2 Comments

.. and I don't know if you can change your format.. but with JSon it is more convenient to use an array[] and field names as you would use in records. In this case: [{ user: testUser1, money:2345 },{user: testUser2, money:23}, ...] all with quotes..
@ChandrachurMukherjee thanks for the edit proposal, my only question is.. did you test it ? Because i recall testing/using the above code.. please confirm, so I can accept.

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.