Android-
我收到以下 JSON 响应。单击特定 ID(1,1015,1016,见下文)时,我有按钮。它将返回内部 Json 对象。
我只在获取特定 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"
}
]
}
]
我写的JSON解析的Java代码如下
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();
}
}
我得到了正确的输出但是 Catch 抛出是因为
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
如果有人想在不捕获异常的情况下获得输出。 那么如何获取内部细节,如 [{"a":"a","b":1,"c ":"1","d":"1-1-1-1","e":"膳食"},{"a":"a","b":6,"c":"2 ","d":"1-1-1","e":"膳食"}]
已编辑:
如果我给出的数字是 1016。它只会解析 1016 [{"a":"a","b":1,"c":"1","d":"1 的内部细节-1-1-1","e":"Meals"}(从整个 JSON 响应中获取)
最佳答案
您的代码无法正常工作,因为在每个 JSONObject
中您都试图找到一个键为 1
的数组,但该数组不可用。因此,在每个 JSONObject
中,您需要使用与数组相关联的正确键。
这样做
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();
}
更新
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();
}
关于java - Android-通过异常解析Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34851520/