Java如何对返回参数进行处理
作者:HelIanthuss
这篇文章主要介绍了Java如何对返回参数进行处理问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
Java对返回参数进行处理
根据返回参数格式获取其中的值
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");java后端常用返回参数,复制粘贴直接用
@Data
public class CommonResult<T> {
/**
* 结果
*/
private T data;
/**
* 状态码
*/
private Integer code;
/**
* 状态码描述
*/
private String message;
public CommonResult() {}
public CommonResult(Integer code, String message) {
this.code = code;
this.message = message;
}
protected CommonResult(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
/**
* 成功返回结果
*
*/
public static <T> CommonResult<T> success() {
return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMessage());
}
/**
* 成功返回结果
*
* @param data 获取的数据
*/
public static <T> CommonResult<T> success(T data ) {
return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMessage(), data);
}
/**
* 成功返回结果
*
* @param data 获取的数据
* @param message 提示信息
*/
public static <T> CommonResult<T> success(T data, String message) {
return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), message, data);
}
/**
* 失败返回结果
* @param errorCode 错误码
* @param message 错误信息
*/
public static <T> CommonResult<T> failed(Integer errorCode, String message) {
return new CommonResult<>(errorCode, message, null);
}
/**
* 失败返回结果
* @param message 提示信息
*/
public static <T> CommonResult<T> failed(String message) {
return new CommonResult<>(ExceptionCode.FAILED.getCode(), message, null);
}
/**
* 权限过期
*/
public static <T> CommonResult<T> unauthorized() {
return new CommonResult<>(ExceptionCode.FAILED.getCode(), "用户登录已过期,请重新登录!", null);
}
}public class ExceptionCode {
public static final ExceptionCode SUCCESS = new ExceptionCode(200, "操作成功");
public static final ExceptionCode FAILED = new ExceptionCode(500, "系统异常");
private int code;
private String message;
public ExceptionCode(int code, String message) {
this.code = code;
this.message= message;
}
public ExceptionCode(String message) {
this.message = message;
}
public Integer getCode() {
return this.code;
}
public String getMessage() {
return this.message;
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
