0

*I am already getting this JSON object from responce.body(). I want every data inside this separately in variable. I am using java.

    {  
   "Classes":{  
      "Primary":{  
         "Classes":{  
            "1":{  
               "section":[  
                  "a",
                  "b"
               ]
            },
            "2":{  
               "sections":[  
                  "a",
                  "b"
               ]
            }
         }
      }
   }
}

*I know how to get the JSONObject but i dont know how can i get that array inside "section". even if i get that array with JSONArray then how to convert it to JSONObject? or String.

*Note that inside value of "section" array is dynamic, value inside that array is dynamic and can be multiiple from "a" to "z". Also JSONObject inside "Classes"(inside primary) is also dynamic. there can be dynamic and multiple "1","2"... and it is string, It is not necessary that there will be incremental numbers.

black-hacker
  • 223
  • 4
  • 12

4 Answers4

1

After 30 mins of war, I find out your answer just copy this code and paste where you want to use -

Here it is -

            String json = "{
              "Classes": {
                "Primary": {
                  "Classes": {
                    "1": {
                      "section": [
                        "a",
                        "b"
                      ]
                    },
                    "2": {
                      "sections": [
                        "a",
                        "b"
                      ]
                    }
                  }
                }
              }
            }";

            try {
                JSONObject jsonObject = new JSONObject(json);
                Log.d("jsonObj", jsonObject.toString());

                JSONObject classJsonObj = jsonObject.optJSONObject("Classes");
                JSONObject primaryJsonObj = classJsonObj.optJSONObject("Primary");
                JSONObject internalClassJsonObj = primaryJsonObj.optJSONObject("Classes");

                if(internalClassJsonObj != null){
                    int i = 1;
                    JSONObject dynmaicInternalJsonObj = null;
                    while (true){
                        dynmaicInternalJsonObj = internalClassJsonObj.optJSONObject(i+"");
                        if(dynmaicInternalJsonObj != null){
                            JSONArray sectionJsonArr = dynmaicInternalJsonObj.optJSONArray("sections");
                            Log.d("SectionArr", sectionJsonArr.toString());

                            // Finally you got your section data here...
                            if(sectionJsonArr != null){
                                for(int j=0; j<sectionJsonArr.length(); j++){
                                    System.out.println("Dynamic Sections Data is: - " + sectionJsonArr.opt(j));
                                }

                                System.out.println("\n\n");
                            }

                        }else{
                            break;
                        }

                        i++;
                    }

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
Al-Amin
  • 1,369
  • 1
  • 9
  • 26
  • Inside that dynmaicInternalJsonObj you are giving "1","2","3",.... like that but it is a string, it is not necessary that there will be incremental numbers there. So, is there any way to go through it via using .lenght()? – black-hacker Mar 22 '19 at 06:13
  • Your answer is most helpful...i solved the problem by adding iterator-loop instead of that while loop. – black-hacker Mar 22 '19 at 06:58
  • Yes you can replace this("1","2","3") incremental key. But you need to change your `Classes` object to json array. If you don't want to change your json structure then you need to take that part(Classes) as HashMap and then you can dynamically parse that data. Here is a clue you may try - https://stackoverflow.com/questions/7304002/how-to-parse-a-dynamic-json-key-in-a-nested-json-result – Al-Amin Mar 22 '19 at 08:14
0

You've tagged this as java so I can only assume you want a solution for that language.

To use this data it needs to parsed, this means you are converting the data into a more usable type.

Click this to learn how to parse JSON in java

0

Assuming you need solution in java, use this to get a object classes for your json structure and then convert your json to java objects using libraries like GSON.

Example:

String json = "{\"city\":\"San Jose\", \"state\": \"CA\", \"country\":\"USA\"}";

Gson gson = new Gson();

Place place = gson.fromJson(json, Place.class);
Logic
  • 2,230
  • 2
  • 24
  • 41
0

If you are using ionic (typescript or javascript), you can use the below approach

var json = "{
          "Classes": {
            "Primary": {
              "Classes": {
                "1": {
                  "section": [
                    "a",
                    "b"
                  ]
                },
                "2": {
                  "sections": [
                    "a",
                    "b"
                  ]
                }
              }
            }
          }
        }";
for(let item in json.Classes.Primary.Classes){
console.log(item.sections);
}

If you want to display the same data in frontend using html, use *ngFor

<div *ngFor="let item in json.Classes.Primary.Classes ">
 <h5>{{item.sections}}</h5>
</div>

I hope it helps.

Akash Chaudhary
  • 701
  • 11
  • 28