Search code examples
c++jsonwxwidgets

wxJSONOBJECTARRAY - accessing the required key value pair


I have just started working with JSON objects with the help of wxJSON. It works fine when I try to work a single JSON object (example given below), it works fine.

 {
   "wxWidgets" : 1
   {
     "Version" :1
     {
       "Major" : 2,
     },
     "Languages" :
     [
       "C++",
       "Python",
       "Perl",
       "C#/Net"
     ]
   }
 }

However, when there are an array of values in the JSON object, I fail to parse the JSON object and extract the required key value pair.

Example :

 [   {
       "wxWidgets" : 1
       {
         "Version" :1
         {
           "Major" : 2,
         },
         "Languages" :
         [
           "C++",
           "Python",
           "Perl",
           "C#/Net"
         ]
       }
     }
     {
       "wxWidgets" : 2
       {
         "Version" :2
         {
           "Major" : 3
         },
         "Languages" :
         [
           "java",
           "j2ee",
           "j2me"
         ]
       }
     }
]

when it is a single object, I access the elements like this:

wxJSONValue root; //this will have the json object created above.
wxString c = root[_T("wxWidgets")][_T("Version")].AsString(); // to get the version.

How do I do it in case there is an array of JSONOBJECTs as given in the second example?


Solution

  • Since you're working with an array,

    root[_T("wxWidgets")][_T("Version")]

    Doesn't deference the first object of the array (index 0 in this case) in the JSON object. The code needs to reflect that you're using an array now, so:

    root[0][_T("wxWidgets")][_T("Version")]