0

Here's my JSON data sample:

        {
   "node_1.1":{
      "someCrap":{
         "someCrap":"SomeValue"
      }
   },
   "node_1.2":{
      "Node_1.2.1":{
         "Node_1.2.1.1":{
            "param1":value,
            "param2":value,
            "param3":"value",
            "paramThatIneed":{
               "ThisIsWhatIActuallyNeed":"url",
               "width":96,
               "height":72
            }
         },
         "Node_1.2.1.2":{
            Same as above, just that paramThatINeed might be missing, gotta place imagePlaceHolder Here
         },
         //and so on... there are a total of 50 of these..

      }
   }
}

Now I could get the node_1.1 and Node 1.2 and the sub-node of it node_1.2.1 However, there are 50 sub-nodes inside of node_1.2.1, and they will have random names returned from the server. Its in string format but they're actually ints. Its a page ID. Now I wanna iterate through the node_1.2.1 and get those sub-nodes and access their sub-nodes and take in the URL of the paramThatINeed. If the paramThatINeed is not present, I need to put some null/dummy value.

This is the code that I tried to work it as far as I've reached:

JSONObject jsonObj = new JSONObject(jsonStr); //jsonStr is the entire JSON string
JsonObject node_1.2= jsonObj.getJsonObject("node_1.1");
JsonObject node_1.2.1 = node_1.2.getJsonObject("node_1.2.1");

What do I do after this? Because I can only getJsonObject by passing a string param to it, I tried using the for loop but it doesn't take any int param.

Also, as I said before, the nodes after that have random names and not fixed. So I'm totally confused.

Please help me out if you know how to solve this problem. Please remember there's no JsonArray in this. I'm probably thinking of editing the JSON string itself and replacing some parts of the '{' with '[' and converting it to an array :( ... I think that's a sad approach.

TN888
  • 7,659
  • 9
  • 48
  • 84
Parikksit Bhisay
  • 163
  • 1
  • 5
  • 20
  • JSON is a key/value pair based system where the keys are fixed values, not data in and of themselves. Is this really the only format of data you have available? – ianhanniballake Feb 20 '14 at 21:05

1 Answers1

1

Use this to iterate over an object. Android (JSONObject) How can I loop through a flat JSON object to get each key and each value but be careful, from json object you won't get the result in original order, like in json array. The result will be in alphabetical order (I hope I was clear). And you can use optJsonobject(), instead of getJsonObject(). It will returns null, instead of throw exception. You can use opt every where instead of get.

Community
  • 1
  • 1
user3057944
  • 701
  • 1
  • 8
  • 19
  • Hey there, thanks a lot for the help, I'm gonna try and implement that and post the result. – Parikksit Bhisay Feb 23 '14 at 03:03
  • I don't have enough rep to thank and vote up ur answer. That was a brilliant tip and thank you very much Sir, you solved the problem. The `optJsonObject()` was a killer idea! :D – Parikksit Bhisay Feb 25 '14 at 08:41
  • Also thank you @Ty221 for editing the question. I appreciate your effort towards keeping stackoverflow a great community and resource. I'll bear in mind the changes you've made and try to include them in my future questions. Thanks :) – Parikksit Bhisay Mar 28 '14 at 20:22