Reading arrays

2008-06-12
2013-04-22
  • Shadi Ibrahem
    Shadi Ibrahem
    2008-06-12

    I haven't been able to read the content of the following JSON from the Value that was parsed,
    I get an exception when I try to read key1 value see last line in code below,

    the JSON input is:
    {
    "todo":
        [
        "work" :
                     {
            "key1" : "value1",
            "key2" : "value2",
                 }
        ]
    }

    this is the code that I use,

            Json::Reader reader;
        Json::Value root;
        reader.parse(jsonString,root);
        Json::Value todos = root["todo"];
        if(!todos_.isArray())
        {
            return;
        }
        Json::Value::iterator itr = todos_.begin();
        while (itr != todos_.end())
        {
            Json::Value single = (*itr);
                    string value = single["key1"];
            }

    please help !

     
    • Shadi Ibrahem
      Shadi Ibrahem
      2008-06-12

      string value = single["key1"].asString();

       
  • cegprakash
    cegprakash
    2013-01-05

    The JSON file itself has a small bug.
    todo is an array and the first element in the array is a pair. Therefore the pair should be enclosed within the braces in order to show that the pair is the first element in the todo array.

    It should be

    {
    "todo":
    [
    {
    "work" :
    {
    "key1" : "value1",
    "key2" : "value2",
    }
    }
    ]
    }