如何在android中我的jsonarray值为null时处理

时间:2017-10-12 09:03:39

标签: android

如何在json数组值中处理这些null

{
    "Code": "0",
    "Message": "Success",
    "District": null,
    "Division": null,
    "Circle": null,
    "PoliceStation": null,
    "Vehicle": [
        {
            "VehicleId": "81",
            "VehicleNo": "Srikakulam II Town - PV1 [AP18P1470]"
        },
        {
            "VehicleId": "82",
            "VehicleNo": "Srikakulam II Town - BC-I [AP18P2325]"
        },
        {
            "VehicleId": "83",
            "VehicleNo": "Srikakulam II Town - BC-II [AP18P2326]"
        },
        {
            "VehicleId": "89",
            "VehicleNo": "Srikakulam II Town - PV2 [AP18P3244]"
        }
    ]
}

2 个答案:

答案 0 :(得分:1)

您可以使用.isNull()方法检查该项目的天气是否为空

  

<强> isNull   在API级别1中添加

     

boolean isNull (String name)

     

如果此对象没有name的映射或者它的值为NULL的映射,则返回true。

示例代码

if(!object.isNull("District")) {
    //District value is not null
}

答案 1 :(得分:0)

请在下面用你的问题解答:

private String optString_1(final JSONObject json, final String key) { return json.isNull(key) ? null : json.optString(key); }