1

Android-

I am getting JSON response following. I have button when i click with Specific ID (1,1015,1016 see below). it will return the inner Json objects.

I have a problem only in getting that Specific Ids(Json)

[
        {
            "1":
            [
                {
                    "a": "a",
                    "b": 1,
                    "c": "1",
                    "d": "1-1-1-1"
                },
                {
                    "a": "a",
                    "b": 6,
                    "c": "2",
                    "d": "1-1-1",
                    "e": "Meals"
                }
            ]
        },
        {
            "1015":
            [
            ]
        },
        {
            "1016":
            [
                {
                    "a": "a",
                    "b": 6,
                    "c": "2",
                    "d": "1-1-1",
                    "e": "Meals1234"
                }
            ]
        },
        {
            "1012":
            [
                {
                    "a": "venky",
                    "b": 6,
                    "c": "2",
                    "d": "1-1-1",
                    "e": "Meals"
                },
                {
                    "a": "venky2",
                    "b": 45,
                    "c": "2",
                    "d": "1-1-1",
                    "e": "Meals"
                }
            ]
        },
        {
            "1011":
            [
                {
                   "a": "a",
                    "b": 6,
                    "c": "2",
                    "d": "1-1-1",
                    "e": "Meals567"
                },
                {
                    "a": "a",
                    "b": 6,
                    "c": "2",
                    "d": "1-1-1",
                    "e": "Meals08676"
                }
            ]
        }
    ]

I write the Java Code for JSON parsing follows

public void load_whole_JsonData() {
        String number = ET_number.getText().toString().trim(); // edittext number is 1 (for example)
        JSONArray jsonArray1;
        JSONObject obj1; 
        JSONArray jsonArray2;
        try {
            jsonArray1=new JSONArray(JsonResponse); // parse the Json response here
            obj1=new JSONObject();
            for (int i = 0; i < jsonArray1.length(); i++) {
                try {
                    jsonArray2= jsonArray1.getJSONObject(i).getJSONArray(number); //number is IDs :  1 , 1015,1016 
                    Log.v("test", "i"+i+ " obj1 "+jsonArray2);
                }
                catch (Exception e){
                    Log.v("test", "exception "+e);
                }
            }

        } catch (JSONException e) {
            Log.v("MTV", "JsonParser exception" + e);
            e.printStackTrace();
        }
    }

I am getting the Correct output but Catch throws because of

jsonArray2 = jsonArray1.getJSONObject(i).getJSONArray(number); //number is IDs :  1 , 1015,1016 

Output (In Logcat):

 V/test: i0 obj1 [{"a":"a","b":1,"c":"1","d":"1-1-1-1","e":"Meals"},{"a":"a","b":6,"c":"2","d":"1-1-1","e":"Meals"}] //This is the output

 V/test: exception org.json.JSONException: No value for 1 //catch exceptions
 V/test: exception org.json.JSONException: No value for 1
 V/test: exception org.json.JSONException: No value for 1
 V/test: exception org.json.JSONException: No value for 1

If anyone have idea to get output without catch exception. then How to get the Inner details like [{"a":"a","b":1,"c":"1","d":"1-1-1-1","e":"Meals"},{"a":"a","b":6,"c":"2","d":"1-1-1","e":"Meals"}]

EDITED:

If i give the number is 1016. it will parse only the Inner details of 1016 [{"a":"a","b":1,"c":"1","d":"1-1-1-1","e":"Meals"} (get from the whole JSON response)

6
  • 2
    You have two variables number with same type String. Are you sure your code is compiling? Commented Jan 18, 2016 at 9:45
  • First correct code in your question. Once place you have written jsonArray2= jsonArray1.getJSONObject(i).getJSONArray(number); and about same thing at another place you have written obj2 = jsonArray1.getJSONObject(i).getJSONArray(number);. If you edit your code before posting then please do it correctly. Commented Jan 18, 2016 at 9:47
  • I just given the number for consideration (String number =1). Now i removed @Rohit5k2 Commented Jan 18, 2016 at 9:50
  • Thanks. I corrected my Code@Rohit5k2 Commented Jan 18, 2016 at 9:51
  • I have corrected the issue in my answer. Please try that. Commented Jan 18, 2016 at 9:59

5 Answers 5

1

Your code is not working because in each JSONObject you are trying to find an array with key 1, which is not available. So in each JSONObject you need to use the correct key with with the array is associated.

Do this

try {
        jsonArray1=new JSONArray(JsonResponse); // parse the Json response here
        obj1=new JSONObject();
        for (int i = 0; i < jsonArray1.length(); i++) {
            try {
                    JSONObject innerJson = jsonArray1.getJSONObject(i);
                    for(Iterator<String> iter = innerJson.keys();iter.hasNext();) 
                    {
                        String key = iter.next();
                        jsonArray2 = innerJson.getJSONArray(key);
                    }
                Log.v("test", "i"+i+ " obj1 "+jsonArray2);
            }
            catch (Exception e){
                Log.v("test", "exception "+e);
            }
        }

    } catch (JSONException e) {
        Log.v("MTV", "JsonParser exception" + e);
        e.printStackTrace();
    }

Update

try {
        jsonArray1=new JSONArray(JsonResponse); // parse the Json response here
        obj1=new JSONObject();
        for (int i = 0; i < jsonArray1.length(); i++) {
            try {
                    JSONObject innerJson = jsonArray1.getJSONObject(i);
                    for(Iterator<String> iter = innerJson.keys();iter.hasNext();) 
                    {
                        String key = iter.next();
                        if(!key.equalIgnoreCase(myKey)) // myKey is the key you want to parse such as 1016
                            continue;
                        jsonArray2 = innerJson.getJSONArray(key);
                    }
                Log.v("test", "i"+i+ " obj1 "+jsonArray2);
            }
            catch (Exception e){
                Log.v("test", "exception "+e);
            }
        }

    } catch (JSONException e) {
        Log.v("MTV", "JsonParser exception" + e);
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

7 Comments

Please try now. keySet method is hidden.
Where I pass the number value ? (1 or 1016 etc)
You don't have to pass it now. The method innerJson.keys() inside the for loop will take care of it automatically.
Early, I got InnerJson but i want only the given Numbers InnerJsons.
If i give the number is 1016. it will parse only the Inner details of 1016 [{"a":"a","b":1,"c":"1","d":"1-1-1-1","e":"Meals"} (refer the whole JSON response)
|
1

Because JSONObject contains dynamic name as key for JSONArray. so JSONObject. keySet to get all keys name then use it to get JSONArray as:

JSONObject jsonObject = jsonArray1.getJSONObject(i); 
Iterator<String> iter = jsonObject.keys();
while (iter.hasNext()) {
    String key = iter.next();
    try {
        JSONArray jsonArray=jsonObject.getJSONArray(key);
    } catch (JSONException e) {
        // Something went wrong!
    }
}

5 Comments

Where I pass the number value ? (1 or 1016 etc)
@VenkateshSelvam: 1 or 1016 is in key and we are passing here JSONArray jsonArray=jsonObject.getJSONArray(key); and jsonArray contains JSONArray
If i give the number is 1016. it will parse only the Inner details of 1016 [{"a":"a","b":1,"c":"1","d":"1-1-1-1","e":"Meals"} (refer the whole JSON response)
@VenkateshSelvam: No only 1016 add Log.i("TAG","JSON :"+key+"--->>"+ jsonArray.toString()); now you will see both JSONArrays in Logs.
0

Only the first object in the array has a property "1". For all others you encounter in the for loop the exception will be thrown.

You may want to check if the property numberexists in the JSON Object before you access it.

3 Comments

Yes,I know. I just the given number is 1 for understanding. The Number is dynamic. So only i ask the question.
Can you explain in your question what you try to achieve?
Thanks, If i give the number is 1016. it will parse only the Inner details of 1016 [{"a":"a","b":1,"c":"1","d":"1-1-1-1","e":"Meals"} (refer the whole JSON response) @henry
0

getJSONArray throws exceptions. Just use optJSONArray instead which returns null in case it doesn't exists.

Comments

0

you are getting exception because you are always accessing array with name"1" the error is in this line of code

 jsonArray2= jsonArray1.getJSONObject(i).getJSONArray(number)

assign 2015,2016 etc also

2 Comments

Please see my questions, i already told "that line gives exception".
If i give the number is 1016. it will parse only the Inner details of 1016 [{"a":"a","b":1,"c":"1","d":"1-1-1-1","e":"Meals"} (refer the whole JSON response)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.