【问题标题】:Android- Json Parsing Through ExceptionAndroid-通过异常解析Json
【发布时间】:2016-01-18 09:42:08
【问题描述】:

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":"Meals"},{"a":"a","b":6,"c":"2","d":"1-1-1","e":"Meals"}]

已编辑:

如果我给出的数字是 1016。它只会解析 1016 的内部细节 [{"a":"a","b":1,"c":"1","d":"1 -1-1-1","e":"Meals"}(从整个 JSON 响应中获取)

【问题讨论】:

  • 您有两个变量number 具有相同的类型String。你确定你的代码正在编译吗?
  • 您问题中的第一个正确代码。一旦你写了jsonArray2= jsonArray1.getJSONObject(i).getJSONArray(number);,在另一个地方你写了obj2 = jsonArray1.getJSONObject(i).getJSONArray(number);。如果您在发布前编辑代码,请正确操作。
  • 我只是给出了考虑的数字(字符串编号=1)。现在我删除了@Rohit5k2
  • 谢谢。我更正了我的 Code@Rohit5k2
  • 我已经在我的回答中更正了这个问题。请尝试一下。

标签: java android json


【解决方案1】:

您的代码不起作用,因为在每个JSONObject 中,您都试图找到一个键为1 的数组,但该数组不可用。所以在每个JSONObject你需要使用正确的key与数组关联。

这样做

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();
    }

【讨论】:

  • 请立即尝试。 keySet 方法被隐藏。
  • 我在哪里传递数值? (1 或 1016 等)
  • 你现在不用通过了。 for 循环内的方法 innerJson.keys() 会自动处理它。
  • 早期,我得到了 InnerJson,但我只想要给定的 Numbers InnerJsons。
  • 如果我给出的数字是 1016。它只会解析 1016 的内部细节 [{"a":"a","b":1,"c":"1"," d":"1-1-1-1","e":"Meals"}(参考整个 JSON 响应)
【解决方案2】:

因为JSONObject 包含动态名称作为JSONArray 的键。所以JSONObject. keySet 获取所有键名然后使用它来获取JSONArray 为:

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!
    }
}

【讨论】:

  • 我在哪里传递数值? (1 或 1016 等)
  • @VenkateshSelvam: 1 或 1016 在 key 中,我们在这里传递 JSONArray jsonArray=jsonObject.getJSONArray(key);jsonArray 包含 JSONArray
  • 如果我给出的数字是 1016。它只会解析 1016 的内部细节 [{"a":"a","b":1,"c":"1"," d":"1-1-1-1","e":"Meals"}(参考整个 JSON 响应)
  • @VenkateshSelvam:不仅1016 添加Log.i("TAG","JSON :"+key+"---&gt;&gt;"+ jsonArray.toString()); 现在您将在日志中看到两个JSONArrays。
【解决方案3】:

只有数组中的第一个对象具有属性"1"。对于您在 for 循环中遇到的所有其他人,将引发异常。

您可能需要在访问之前检查属性number是否存在于 JSON 对象中。

【讨论】:

  • 是的,我知道。我只是给定的数字是 1 以便理解。数字是动态的。所以只有我问这个问题。
  • 你能在你的问题中解释你想要达到的目标吗?
  • 谢谢,如果我给出的数字是 1016。它只会解析 1016 的内部细节 [{"a":"a","b":1,"c":"1" ,"d":"1-1-1-1","e":"Meals"} (参考整个 JSON 响应)@henry
【解决方案4】:

getJSONArray 抛出异常。只需使用 optJSONArray 代替它返回 null 以防万一它不存在。

【讨论】:

    【解决方案5】:

    您遇到异常是因为您总是访问名称为“1”的数组,错误就在这行代码中

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

    也分配 2015,2016 等

    【讨论】:

    • 请看我的问题,我已经告诉过“那行给出了例外”。
    • 如果我给出的数字是 1016。它只会解析 1016 的内部细节 [{"a":"a","b":1,"c":"1"," d":"1-1-1-1","e":"Meals"}(参考整个 JSON 响应)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2012-03-30
    相关资源
    最近更新 更多