【问题标题】:Android JSON Parse Getting Array within ArrayAndroid JSON Parse 在数组中获取数组
【发布时间】:2015-10-29 13:54:57
【问题描述】:

似乎是一个简单的问题,我知道已经有多个这样的问题。我正在尝试从数组中的数组获取 JSON 数据。

我有一个 JSON 数组如下:

{
"results": [
    {
        "name": "John Doe",
        "time": "09:00:00",
        "claim": {
            "date": "2015-08-06",
            "comments": "Some comments"
        }
    },
    {
        "name": "Mary Smith",
        "time": "10:00:00",
        "claim": {
            "date": "2015-08-06",
            "comments": "Some comments"
        }
        }
    }
]
}

我正在尝试从中解析出所有信息,我能够获取nametime,但我无法从claim 数组中获取数据。

我在 SO 上查看了多个类似的问题,但他们的解决方案都没有解决我的问题。

这是我的 Java 代码:

@Override
    protected void onPostExecute(JSONObject json) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        try {
            // Getting JSON Array from URL
            JSONArray android = json.getJSONArray(TAG_RESULTS);
            for (int i = 0; i < android.length(); i++) {
                JSONObject c = android.getJSONObject(i);

                // Storing  JSON items in Variables
                String time = c.getString(TAG_TIME);

                //For loop for claims array:
                JSONArray claims = c.getJSONArray(TAG_CLAIM);
                for (int j = 0; j < claims.length(); j++) {
                    JSONObject d = claims.getJSONObject(j);
                    String name = d.getString(TAG_NAME);
                 //other parsing and list view.

我收到此错误:

org.json.JSONException: Value {"date": "2015-08-06","comments": "Some comments"} at claim of type org.json.JSONObject cannot be converted to JSONArray
08-06 12:43:27.436  17586-17586/com.murrion.navigationdrawer W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:100)
08-06 12:43:27.436  17586-17586/com.murrion.navigationdrawer W/System.err﹕ at org.json.JSONObject.getJSONArray(JSONObject.java:588)

在这条线上:JSONArray claims = c.getJSONArray(TAG_CLAIM);

我不确定出了什么问题,我非常感谢一些帮助。谢谢

【问题讨论】:

  • 请学习 json 的基础知识...显然:{ .... } 不是数组
  • 你的 json 格式是错误的,检查一下你上面发布的内容是否有额外的 } 大括号,并且还声称不是数组它的对象,所以只需从 jsonObject 调用它

标签: java android arrays json parsing


【解决方案1】:

claim 不是数组,而是另一个对象。如果你希望它是一个数组,它应该是这样的:

    "claim": [
        "date": "2015-08-06",
        "comments": "Some comments"
    ]

(注意[ ] 而不是{ }

如果它是一个对象,那么代码应该是:

            JSONObject claims = c.getJSONObject(TAG_CLAIM);

【讨论】:

  • 你的 json 格式甚至是错误的 在他的情况下它应该是 {.....his rest object and "claim" : [{ "date" : "2015-08-06", "cmets " : "一些 cmets" } ] }
  • 一般会是这样 [{"date":"2015-08-06","comment":"some cmets"},{"date":"2015-08- 07","comment":"some cmets1"}] or [{"date":"2015-08-06","comment":"some cmets"} ] or [] this
  • @Naval,相关的一点是他试图将一个对象解析为一个数组。在指出这一事实后,他可以修复他的代码以使其与 json 对象匹配或以其他方式匹配。答案不是做所有的工作,而是指出解决问题的方法。
  • ,是的,您是对的,我们必须指出解决方案的方法,您引用的示例是错误的,但解释了问题并给出了解决方案是正确的,您通过编写 JSONObject 声明完成了他的全部工作= c.getJSONObject(TAG_CLAIM);这条线老兄,即使你看到他的 json 不正确也可能是错字问题,但整个问题只从他们开始
【解决方案2】:

试试这个

{
"results": [
    {
        "name": "John Doe",
        "time": "09:00:00",
        "claim": [

            "date": "2015-08-06",
            "comments": "Some comments"
        ]
    },
    {
        "name": "Mary Smith",
        "time": "10:00:00",
        "claim": [
            "date": "2015-08-06",
            "comments": "Some comments"
        ]

    }
]
}


注意:错误堆栈跟踪中明确提到了您的 json 中的问题,它告诉您正在尝试将 json 对象(在 {...} 内)解析为json 数组(数组应该在 [...] 内,而不是 {...})。所以请尝试阅读堆栈跟踪。

【讨论】:

  • 这种 json 格式也是错误的,伙计 "claim": [ "date": "2015-08-06", "cmets": "Some cmets" ] 这甚至不是数组,请参阅我上面的评论在@Sebastian 中这应该是这样的 jsonObject "claim": [{ "date": "2015-08-06", "cmets": "Some cmets" } ] 所以你可以通过名字 "claim" 来调用这个数组删除名称,它将成为 jsonarray
  • 是的...我经历过。并理解我的错误。谢谢。
【解决方案3】:

去掉多余的}花括号后你的json会是这样的

   {
        "results" :
        [
            {
                "name" : "John Doe",
                "time" : "09:00:00",
                "claim" : 
                {
                    "date" : "2015-08-06",
                    "comments" : "Some comments"
                }
            }, 
            {
                "name" : "Mary Smith",
                "time" : "10:00:00",
                "claim" : 
                {
                    "date" : "2015-08-06",
                    "comments" : "Some comments"
                }
            }
        ]
    }
and to parse this json your code like this
@Override
protected void onPostExecute(JSONObject json) 
{
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        try 
        {
            // Getting JSON Array from URL
            JSONArray android = json.getJSONArray(TAG_RESULTS);
            for (int i = 0; i < android.length(); i++){
                JSONObject c = android.getJSONObject(i);    
                // Storing  JSON items in Variables
                String time = c.getString(TAG_TIME);
                String time = c.getString("name");    
                //  for claims  object:
                JSONObject claims = c.getJSONObject(TAG_CLAIM);
                    String date= claims .getString("date");
                    String comments= claims .getString("comments");
                 //other parsing and list view.
             }
         }
         catch(Exception e){}
}

正如你所说,你的 json 应该是这样的

{
    "results" :
    [{
            "name" : "John Doe",
            "time" : "09:00:00",
            "claim" : [{
                    "date" : "2015-08-06",
                    "comments" : "Some comments"
                }
            ]
        }, {
            "name" : "Mary Smith",
            "time" : "10:00:00",
            "claim" : [{
                    "date" : "2015-08-06",
                    "comments" : "Some comments"
                }
            ]
        }
    ]
}

你的代码是这样的

@Override
    protected void onPostExecute(JSONObject json) 
    {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            try 
            {
                // Getting JSON Array from URL
                JSONArray android = json.getJSONArray(TAG_RESULTS);
                for (int i = 0; i < android.length(); i++){
                    JSONObject c = android.getJSONObject(i);    
                    // Storing  JSON items in Variables
                    String time = c.getString(TAG_TIME);
                    String time = c.getString("name");    
                    //  for claims  object:
                    JSONArray claims = c.getJSONArray(TAG_CLAIM);
                    for (int j = 0; j < claims.length(); j++) {
                       JSONObject d = claims.getJSONObject(j);
                        String date= d.getString("date");
                        String comments= d.getString("comments");
                     //other parsing and list view.
                     }
                 }
             }
             catch(Exception e){}
    }

【讨论】:

    猜你喜欢
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    相关资源
    最近更新 更多