Android-Android- JSON解析通過異常
我得到以下JSON響應。當我點擊特定ID時,我有按鈕(1,1015,1016見下文)。它會返回內部的Json對象。
我只得到那個特定的ID(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"
}
]
}
]
我寫的Java代碼JSON解析如下
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();
}
}
我得到正確的輸出,但趕上拋出,因爲有問題的
jsonArray2 = jsonArray1.getJSONObject(i).getJSONArray(number); //number is IDs : 1 , 1015,1016
輸出(在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
如果有人有想法得到沒有catch異常的輸出。那麼如何獲得內部細節,如[{"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"}]
編輯:
如果我給的數目是1016它將解析只有1016的內部細節[{「一」:「一個」,」 b「:1,」c「:」1「,」d「:」1-1-1-1「,」e「:」Meals「}(從整個JSON響應中獲得)
你有兩個變量'number'具有相同類型'String'。你確定你的代碼正在編譯? – Rohit5k2
您的問題中的第一個正確的代碼。一旦你寫了'jsonArray2 = jsonArray1.getJSONObject(i).getJSONArray(number);'和在你寫的另一個地方大致相同的東西'obj2 = jsonArray1.getJSONObject(i).getJSONArray(number);'。如果您在發佈之前編輯您的代碼,請正確使用它。 – Rohit5k2
我剛剛給出了考慮的數字(字符串數字= 1)。現在我刪除了@ Rohit5k2 –