這篇文章主要介紹了Java如何對(duì)返回參數(shù)進(jìn)行處理問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
Java對(duì)返回參數(shù)進(jìn)行處理
根據(jù)返回參數(shù)格式獲取其中的值
1.得到ResponseEntity<String> responseEntity對(duì)象
import org.springframework.http.ResponseEntity;
得到ResponseEntity<String> responseEntity對(duì)象
<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.根據(jù)ResponseEntity<String> responseEntity對(duì)象
獲取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字符串轉(zhuǎn)化為json對(duì)象
//將json字符串轉(zhuǎn)化為json對(duì)象
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
}
//取出data部分對(duì)象
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
}
]
}
list中的內(nèi)容帶有中括號(hào)[],所以要轉(zhuǎn)化為JSONArray類型的對(duì)象
//轉(zhuǎn)化為JSONArray類型的對(duì)象
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
}
]
jsonArray.getJSONObject(index)
//隨機(jī)選取一個(gè)數(shù)組
JSONObject idInfo = jsonArray.getJSONObject(randomInteger(0,jsonArray.size()));
String id=idInfo.getString("id");
java后端常用返回參數(shù),復(fù)制粘貼直接用
@Data
public class CommonResult<T> {
/**
* 結(jié)果
*/
private T data;
/**
* 狀態(tài)碼
*/
private Integer code;
/**
* 狀態(tài)碼描述
*/
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;
}
/**
* 成功返回結(jié)果
*
*/
public static <T> CommonResult<T> success() {
return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMessage());
}
/**
* 成功返回結(jié)果
*
* @param data 獲取的數(shù)據(jù)
*/
public static <T> CommonResult<T> success(T data ) {
return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMessage(), data);
}
/**
* 成功返回結(jié)果
*
* @param data 獲取的數(shù)據(jù)
* @param message 提示信息
*/
public static <T> CommonResult<T> success(T data, String message) {
return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), message, data);
}
/**
* 失敗返回結(jié)果
* @param errorCode 錯(cuò)誤碼
* @param message 錯(cuò)誤信息
*/
public static <T> CommonResult<T> failed(Integer errorCode, String message) {
return new CommonResult<>(errorCode, message, null);
}
/**
* 失敗返回結(jié)果
* @param message 提示信息
*/
public static <T> CommonResult<T> failed(String message) {
return new CommonResult<>(ExceptionCode.FAILED.getCode(), message, null);
}
/**
* 權(quán)限過期
*/
public static <T> CommonResult<T> unauthorized() {
return new CommonResult<>(ExceptionCode.FAILED.getCode(), "用戶登錄已過期,請(qǐng)重新登錄!", null);
}
}
public class ExceptionCode {
public static final ExceptionCode SUCCESS = new ExceptionCode(200, "操作成功");
public static final ExceptionCode FAILED = new ExceptionCode(500, "系統(tǒng)異常");
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;
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。