Java对返回参数进行处理(JSONObject,getJSONArray等)

转载于:https://www.cnblogs.com/chushujin/p/11371450.html

Java对返回参数进行处理(JSONObject,getJSONArray等)

 

一、根据返回参数格式获取其中的值

1.得到ResponseEntity<String> responseEntity对象

复制代码
import org.springframework.http.ResponseEntity;

得到ResponseEntity<String> responseEntity对象


<200,

{
    "code":0,
    "data":{
        "list":[
            {
                "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
}

,{Server=[Tengine/2.1.1], Date=[Sun, 18 Aug 2019 00:54:32 GMT], Content-Type=[application/json;charset=UTF-8], Content-Length=[412], Connection=[keep-alive]}>
复制代码

2.根据ResponseEntity<String> responseEntity对象,获取body部分,body为json格式字符串

String content = responseEntity.getBody();


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

3.获取list中的id,name,mobile等字段值

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

  输出

复制代码
{
    "msg":"success",
    "code":0,
    "data":{
        "list":[
            {
                "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
}
复制代码

 3.2 取出data部分

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

输出

复制代码
{
    "list":[
        {
            "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.3 data中包含有数组,list中的内容带有中括号[],所以要转化为JSONArray类型的对象

//转化为JSONArray类型的对象
JSONArray jsonArray = data.getJSONArray("list");

输出;

复制代码
[
    {
        "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.4 若为多个数组

jsonArray.getJSONObject(index)
//随机选取一个数组
JSONObject idInfo = jsonArray.getJSONObject(randomInteger(0,jsonArray.size()));
String id=idInfo.getString("id");
原文地址:https://www.cnblogs.com/lwh-12345/p/14782508.html