0

I need to search for an element in a json file using the jsoncpp library. I can't wrap my head around how to get to the most inner array... Any thoughts?

{
    "key": int,
    "array1": [
        {
            "key": int,
            "array2": [
                {
                    "key": int,
                    "array3": [
                        {
                            "needed_key": int
                        }
                    ]
                }
             ]
          }
     ]
}

Up until now I tried something like this:

    const Json::Value& array1 = root["array1"];
    for (size_t i = 0; i < array1.size(); i++)
    {
        const Json::Value& array2 = array1["array2"];
        for (size_t j = 0; j < array2.size(); j++)
        {
            const Json::Value& array3 = array2["array3"];
            for (size_t k = 0; k < array3.size(); k++)
            {
                std::cout << "Needed Key: " << array3["needed_key"].asInt();
            }
        }
    }

But it throws:

JSONCPP_NORETURN void throwLogicError(String const& msg) {
  throw LogicError(msg);
}
5
  • 1
    Show what you have tried and the errors and/or unexpected behaviour you get. Commented Apr 26, 2022 at 7:49
  • I have updated the post. I know what I have by now is rubbish, but I am out of ideas :( Commented Apr 26, 2022 at 8:21
  • 1
    You iterate through an array with ints. So shouldn't it be something like array2 = array1[i]["array2"] and so on? Commented Apr 26, 2022 at 9:08
  • do you need to be checking something like array1["key"] == key1 at any level? How do you know which element contains the next level array? Commented Apr 26, 2022 at 9:31
  • @Caleth, yes. For example if the second key is equal to a certain value, then extract the needed key. But for now that is not my focus. I just want to find a way to get any element from the inner most array. After looking all day , I think that I need to to what is called a deserialization. Commented Apr 26, 2022 at 12:51

1 Answer 1

1

You can't access array2 with array1["array2"], since array1 contains an array of objects, not an object, so you should get array2 with an index i, array1[i]["array2"] instead.

The following code works for me:

  const Json::Value &array1 = root["array1"];
  for (int i = 0; i < array1.size(); i++) {
    const Json::Value &array2 = array1[i]["array2"];
    for (int j = 0; j < array2.size(); j++) {
      const Json::Value &array3 = array2[j]["array3"];
      for (int k = 0; k < array3.size(); k++) {
        std::cout << "Needed Key: " << array3[k]["needed_key"].asInt();
      }
    }
  }

The output looks like:

Needed Key: 4
Sign up to request clarification or add additional context in comments.

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.