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)
numberwith same typeString. Are you sure your code is compiling?jsonArray2= jsonArray1.getJSONObject(i).getJSONArray(number);and about same thing at another place you have writtenobj2 = jsonArray1.getJSONObject(i).getJSONArray(number);. If you edit your code before posting then please do it correctly.