-5

I've checked almost all posts in SO, but i didn't get the solution

Question: I've the JSON like below

{
  "address": {
    "state": "World",
    "address": "infinite space, 000",
    "city": "Android city",
    "address2": {
      "state": "World2",
      "address": "infinite space2, 002",
      "city": "Android city2",
      "address3": {
        "state": "World3",
        "address": "infinite space3, 003",
        "city": "Android city3"
      }
    }
  },
  "valid": {
    "state": "World",
    "address": "infinite space, 000",
    "city": "Android city",
    "valid2": {
      "state": "World2",
      "address": "infinite space2, 002",
      "city": "Android city2",
      "valid3": {
        "state": "World3",
        "address": "infinite space3, 003",
        "city": "Android city3"
      }
    }
  }
}

In this, every object name was unique and in future i may have many nested JSON objects also.

My requirement is: I want to parse every nested JSON object dynamically.

For example: If i pass any object name. My method have to return every data (key and value) of that object or Suppurate every nested object from the JSON and maintain them supperatly

Bahu
  • 1,516
  • 2
  • 28
  • 49
  • seems pretty straight forward. The json you posted doesnt look valid either...but anyway.. do you know what the key is all the time? you woul dhave to iterate through while(key.hasNext()) if (key.equals(string){do something} – letsCode Jan 24 '18 at 14:35
  • It's a valid `JSON` but not an authorized one. If i use `hasNext()` i can parse it but i have to loop the condition for inner objects. Other than than this do you have any better solution – Bahu Jan 24 '18 at 14:43
  • @down voters: Tell the wrong in this question then i won't repeat it again but without commenting why u people are down voting – Bahu Jan 24 '18 at 14:45
  • you are being down voted because we like to see what solution you had and then we can help fix the code instead of us giving you the solution. are you creating the json and storing it? is it static or does it change? if its static, maybe re design it into a simple array. it would be quicker PS -> i didnt downvote. :) – letsCode Jan 24 '18 at 14:49
  • @DroiDev Thank you for ur idea. I'll try in your way and much thanks for not down voting my question ;-) – Bahu Jan 24 '18 at 15:34
  • @MohamedMohaideenAH Thank you. I've missed it. Few min i'll try it and let you know – Bahu Jan 24 '18 at 15:35
  • no worries. if the key is dynamic, you have to use key.hasnext() in order to find the key and its value. – letsCode Jan 24 '18 at 15:35
  • Yeah i've idea on this but haven't provided that here. That laziness cost 5 down votes ;-) – Bahu Jan 24 '18 at 15:37

2 Answers2

0

the best option is work with class and not with Json Objec. I recommend you Gson Library for convert json response to a Class

Gson https://github.com/google/gson

if you made a request to server also Gson + Retrofit 2 are excellent option.

Retrofit 2 http://square.github.io/retrofit/

Emmanuel Montt
  • 346
  • 1
  • 6
  • Hi Emmanuel Montiel Martínez, Thank you for your valuable response. If you don't mind can you give any example – Bahu Jan 24 '18 at 16:21
  • Follow this tutorial https://code.tutsplus.com/tutorials/getting-started-with-retrofit-2--cms-27792 You can search others tutorials about Retrofit 2. the first time is somewhat tedious but in the end it are very good option to make requests to the server – Emmanuel Montt Jan 24 '18 at 16:29
  • Thank you Emmanuel Montiel Martínez:-) – Bahu Jan 24 '18 at 16:30
0

Thank you DroiDev and MohamedMohaideenAH. Finally i got the solution

private void parseJson(JSONObject jsonObject){

    try {

        for(int i = 0; i < jsonObject.length(); i++){


            if(jsonObject.get(jsonObject.names().getString(i)) instanceof JSONObject){

                Log.e("===Start===", "===Start===");

                Log.e("objectName", jsonObject.names().getString(i));

                JSONObject  singleObj = new JSONObject(jsonObject.get(jsonObject.names().getString(i)).toString());

                Iterator<String> keys= singleObj.keys();
                while (keys.hasNext()){
                    String keyValue = keys.next();
                    String valueString = singleObj.getString(keyValue);

                    if(!isJSONObjectOrString(valueString))
                        Log.e(keyValue, valueString);
                }

                Log.e("===End===", "===End===");

                parseJson(singleObj);
            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

private boolean isJSONObjectOrString(String str) {
    try {
        new JSONObject(str);
    } catch (JSONException e) {
        //e.printStackTrace(); //If you uncomment, log will confuse you
        return false;//If string
    }
    return true;//If JSONObject
}
Bahu
  • 1,516
  • 2
  • 28
  • 49