0

Is there a "proper" way to extract an element of array which is a child of an object in JsonCPP?

I'm dealing with JsonRPC objects along the lines of:

{
    "jsonrpc": "2.0",
    "id": 1,
    "result": {
        "data": [{
            "state": 7,
            "stateticks":362220
        }],
        "ticks": 416410
    }
}

If I want to pull the first (or, n-th, for that matter) element of the array, by doing

  Json::Value& sessionData = object["result"]["data"][0]; 

I'm getting the error of:

 error: ambiguous overload for ‘operator[]’ (operand types are ‘const Json::Value’ and ‘int’)
 116 |                 Json::Value& sessionData = object["result"]["data"][0];

I can bypass this by creating an intermediate variable:

  Json::Value& allSessionData = object["result"]["data"]; 
  Json::Value& sessionData = allSessionData[0]; 

but I'm also dealing with nested arrays and generally I don't like multiplying helper variables like that and I bet there is a better way... can you help?

4
  • Which jsoncpp version and which compiler? Do you get the same error if you use object["result"]["data"][0u]? Commented Sep 22, 2021 at 13:51
  • @Michael Wish I knew where to find it, no version# in any header - but 0u works. I still wonder what type is the generic literal 0 so that Jsoncpp doesn't recognize it as int. Commented Sep 22, 2021 at 14:16
  • json.h contains the version number. But the compiler may also matter. I can't reproduce this with jsoncpp 1.8.4 and Clang. Commented Sep 22, 2021 at 14:18
  • @Michael my json.h is 8 lines, literally code guards and 4 includes. Maybe my jsoncpp is just so old - the project has been around for a good while. Regardless, 0u or just casting to int solves my problem. Commented Sep 22, 2021 at 14:59

0

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.