如何在json中读取null数组

时间:2014-10-13 09:54:09

标签: java json

如何读取某个时间值包含NULL的数组。这是我的json回复

{
    "result_count": 7,
    "total_count": "7",
    "next_offset": 7,
    "entry_list": [
        {
            "id": "1b9ebe42-e0c0-fa07-627f-51ff9db86bf7",
            "module_name": "man_payment_reminder",
            "name_value_list": {
                "remind_date_c": {
                    "name": "remind_date_c",
                    "value": "2013-08-08"
                },
                "status_c": {
                    "name": "status_c",
                    "value": "Open</span>"
                },
                "description": {
                    "name": "description",
                    "value": ""
                }
            }
        },
        {
            "id": "1e3b7728-ec1c-b697-50e6-5333fe3c8122",
            "module_name": "man_payment_reminder",
            "name_value_list": {
                "remind_date_c": {
                    "name": "remind_date_c",
                    "value": "2014-03-29"
                },
                "status_c": {
                    "name": "status_c",
                    "value": "Open</span>"
                },
                "description": {
                    "name": "description",
                    "value": "call for next payment"
                }
            }
        },
        {
            "id": "2da94099-b02a-fb71-6e8b-533e9fe9270b",
            "module_name": "man_payment_reminder",
            "name_value_list": {
                "remind_date_c": {
                    "name": "remind_date_c",
                    "value": "2014-04-16"
                },
                "status_c": {
                    "name": "status_c",
                    "value": "Open</span>"
                },
                "description": {
                    "name": "description",
                    "value": "Call After 15 Days "
                }
            }
        },
        {
            "id": "5b33080d-419e-58d1-bd0b-53fed4bfa929",
            "module_name": "man_payment_reminder",
            "name_value_list": {
                "remind_date_c": {
                    "name": "remind_date_c",
                    "value": "2014-08-28"
                },
                "status_c": {
                    "name": "status_c",
                    "value": "Closed</span>"
                },
                "description": {
                    "name": "description",
                    "value": ""
                }
            }
        },
        {
            "id": "694d8ee2-4dab-ddbc-5340-533e9f09647e",
            "module_name": "man_payment_reminder",
            "name_value_list": {
                "remind_date_c": {
                    "name": "remind_date_c",
                    "value": "2014-04-16"
                },
                "status_c": {
                    "name": "status_c",
                    "value": "Open</span>"
                },
                "description": {
                    "name": "description",
                    "value": "Call After 15 Days "
                }
            }
        },
        {
            "id": "9538895f-a4ea-8c0f-b574-51f3a4bb23df",
            "module_name": "man_payment_reminder",
            "name_value_list": {
                "remind_date_c": {
                    "name": "remind_date_c",
                    "value": "2013-07-27"
                },
                "status_c": {
                    "name": "status_c",
                    "value": "Closed</span>"
                },
                "description": {
                    "name": "description",
                    "value": ""
                }
            }
        },
        {
            "id": "aad959b2-3241-5b77-d0b5-5334125a733e",
            "module_name": "man_payment_reminder",
            "name_value_list": {
                "remind_date_c": {
                    "name": "remind_date_c",
                    "value": "2014-03-27"
                },
                "status_c": {
                    "name": "status_c",
                    "value": "Open</span>"
                },
                "description": {
                    "name": "description",
                    "value": "call for payment"
                }
            }
        }
    ],
    "relationship_list": [
        {
            "link_list": []
        },
        {
            "link_list": []
        },
        {
            "link_list": [
                {
                    "name": "man_odrer_list_man_payment_reminder_1",
                    "records": [
                        {
                            "link_value": {
                                "name": {
                                    "name": "name",
                                    "value": "1400003"
                                }
                            }
                        }
                    ]
                }
            ]
        },
        {
            "link_list": [
                {
                    "name": "man_odrer_list_man_payment_reminder_1",
                    "records": [
                        {
                            "link_value": {
                                "name": {
                                    "name": "name",
                                    "value": "1400088"
                                }
                            }
                        }
                    ]
                }
            ]
        },
        {
            "link_list": [
                {
                    "name": "man_odrer_list_man_payment_reminder_1",
                    "records": [
                        {
                            "link_value": {
                                "name": {
                                    "name": "name",
                                    "value": "1400003"
                                }
                            }
                        }
                    ]
                }
            ]
        },
        {
            "link_list": []
        },
        {
            "link_list": []
        }
    ]
}

我的代码是这样的

JSONObject object = new JSONObject(result);

        JSONArray no1 = object.getJSONArray("relationship_list");
         JSONArray no = object.getJSONArray("entry_list");
         for(int j=0;j<no.length();j++){

        JSONObject obj = no.getJSONObject(j);

        JSONObject namevalue = obj.getJSONObject("name_value_list");

        JSONObject objid = namevalue.getJSONObject("remind_date_c");
        String remaindmeon= objid.getString("value");

        JSONObject objname = namevalue.getJSONObject("status_c");
        String status1= objname.getString("value");

        Spanned status = Html.fromHtml( status1 );

     JSONObject objqua = namevalue.getJSONObject("description");
     String description = objqua.getString("value");

     Log.v("description",""+description);


    /*JSONObject objunit = namevalue.getJSONObject("unit_price_c");
    String unitprice = objunit.getString("value");

    unitprice = unitprice.split("\\.")[0];

    JSONObject objtotal = namevalue.getJSONObject("total_price_c");
    String totalprice = objtotal.getString("value");

    totalprice = totalprice.split("\\.")[0];

    JSONObject objdes = namevalue.getJSONObject("description");
    String description = objdes.getString("value");*/

    JSONObject objrel = no1.getJSONObject(j);

    Log.v("objrel",""+objrel);

    if(objrel.isNull("link_list") == true){

        JSONArray link = objrel.getJSONArray("link_list");

        JSONObject objlink = link.getJSONObject(0);

        JSONArray records = objlink.getJSONArray("records");

        JSONObject objrecords = records.getJSONObject(0);

    Log.v("objrecords",""+objrecords);

        JSONObject objlinkvalue = objrecords.getJSONObject("link_value");

        JSONObject objlinkname = objlinkvalue.getJSONObject("name");
         orderno = objlinkname.getString("value");

        JSONObject objlinkid = objlinkvalue.getJSONObject("id");
        orderid = objlinkid.getString("value");

    }

2 个答案:

答案 0 :(得分:0)

我猜你应该在这里测试空数组:

JSONArray link = objrel.getJSONArray("link_list");
If (!link.isNull() && link.length>0) {
      JSONObject objlink = link.getJSONObject(0);
}

答案 1 :(得分:0)

此检查

if(objrel.isNull("link_list") == true)

应该是

if(!objrel.isNull("link_list"))