Java对返回参数进行处理(JSONObject.parseObject、getJSONObject、getJSONArray)

原始json:

{
    "code":0,
    "data":{
          [
            {
                "amount":0,
                "auditTime":"",
                "channelType":"",
                "createTime":"2019-08-13 17:01:55",
                "creditStatus":"",
                "edit":true,
                "fundsStatus":"",
                "id":372,
                "idNo":"",
                "lendRequestId":0,
                "mobile":"13289989000",
                "name":"客户姓名",
                "soinsStatus":"",
                "state":0,
                "stateText":"",
                "viewStateText":0
            }
        ]
    },
    "mask":"251eeedb-e214-47c6-aa0c-3eb6c7b67aa0",
    "msg":"success",
    "timestamp":1566089672
}

=============================================================================================================================================

1、将json字符串转化为json对象

//将json字符串转化为json对象
JSONObject json = JSONObject.parseObject(content);

  输出

 

{
    "msg":"success",
    "code":0,
    "data":{
          [
            {
                "amount":0,
                "soinsStatus":"",
                "viewStateText":0,
                "edit":true,
                "mobile":"12324435555",
                "channelType":"",
                "creditStatus":"",
                "fundsStatus":"",
                "idNo":"",
                "auditTime":"",
                "createTime":"2019-08-13 17:01:55",
                "stateText":"",
                "name":"客户姓名",
                "id":372,
                "lendRequestId":0,
                "state":0
            }
        ]
    },
    "mask":"251eeedb-e214-47c6-aa0c-3eb6c7b67aa0",
    "timestamp":1566089672
}

 

2、取出data部分

//取出data部分对象
JSONObject data = json.getJSONObject("data");

输出:

 

{
     [
        {
            "amount":0,
            "soinsStatus":"",
            "viewStateText":0,
            "edit":true,
            "mobile":"13234444555",
            "channelType":"",
            "creditStatus":"",
            "fundsStatus":"",
            "idNo":"",
            "auditTime":"",
            "createTime":"2019-08-13 17:01:55",
            "stateText":"",
            "name":"客户姓名",
            "id":372,
            "lendRequestId":0,
            "state":0
        }
    ]
}

 

3、data中包含有数组,list中的内容带有中括号[],所以要转化为JSONArray类型的对象

//如果要取出amount的值
JSONArray jsonArray = data.getJSONArray("data").getJSONObject(0).getString("amount");

输出:

[
    {
        "amount":0,
        "soinsStatus":"",
        "viewStateText":0,
        "edit":true,
        "mobile":"13234444555",
        "channelType":"",
        "creditStatus":"",
        "fundsStatus":"",
        "idNo":"",
        "auditTime":"",
        "createTime":"2019-08-13 17:01:55",
        "stateText":"",
        "name":"客户姓名",
        "id":372,
        "lendRequestId":0,
        "state":0
    }
]

 

4、若为多个数组

jsonArray.getJSONObject(index)
//随机选取一个数组
JSONObject idInfo = jsonArray.getJSONObject(randomInteger(0,jsonArray.size()));
String id=idInfo.getString("id");

 


版权声明:本文为cyjs1988原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。