Java如何對(duì)返回參數(shù)進(jìn)行處理
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":"客戶(hù)姓名",
"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":"客戶(hù)姓名",
"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":"客戶(hù)姓名",
"id":372,
"lendRequestId":0,
"state":0
}
]
},
"mask":"251eeedb-e214-47c6-aa0c-3eb6c7b67aa0",
"timestamp":1566089672
}- 3.2 取出data部分
//取出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":"客戶(hù)姓名",
"id":372,
"lendRequestId":0,
"state":0
}
]
}- 3.3 data中包含有數(shù)組
list中的內(nèi)容帶有中括號(hào)[],所以要轉(zhuǎn)化為JSONArray類(lèi)型的對(duì)象
//轉(zhuǎn)化為JSONArray類(lèi)型的對(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":"客戶(hù)姓名",
"id":372,
"lendRequestId":0,
"state":0
}
]- 3.4 若為多個(gè)數(shù)組
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)限過(guò)期
*/
public static <T> CommonResult<T> unauthorized() {
return new CommonResult<>(ExceptionCode.FAILED.getCode(), "用戶(hù)登錄已過(guò)期,請(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è)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Servlet編程第一步之從零構(gòu)建Hello?World應(yīng)用詳細(xì)步驟+圖解
本文詳細(xì)介紹了Servlet和maven的基本概念及其在JavaWeb開(kāi)發(fā)中的應(yīng)用,首先解釋了Servlet是一個(gè)在服務(wù)器上處理請(qǐng)求的Java程序,然后介紹了maven作為管理和構(gòu)建Java項(xiàng)目的工具,需要的朋友可以參考下2024-10-10
SpringBoot如何使用@Cacheable進(jìn)行緩存與取值
這篇文章主要介紹了SpringBoot如何使用@Cacheable進(jìn)行緩存與取值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
springboot實(shí)現(xiàn)excel表格導(dǎo)出幾種常見(jiàn)方法
在日常的開(kāi)發(fā)中避免不了操作Excel,下面這篇文章主要給大家介紹了關(guān)于springboot實(shí)現(xiàn)excel表格導(dǎo)出的幾種常見(jiàn)方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
SpringBoot應(yīng)用中出現(xiàn)的Full GC問(wèn)題的場(chǎng)景與解決
這篇文章主要為大家詳細(xì)介紹了SpringBoot應(yīng)用中出現(xiàn)的Full GC問(wèn)題的場(chǎng)景與解決方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
一文搞懂Java?ScheduledExecutorService的使用
JUC包(java.util.concurrent)中提供了對(duì)定時(shí)任務(wù)的支持,即ScheduledExecutorService接口。本文主要對(duì)ScheduledExecutorService的使用進(jìn)行簡(jiǎn)單的介紹,需要的可以參考一下2022-11-11
intelliJ idea 2023 配置Tomcat 8圖文教程
這篇文章主要介紹了intelliJ idea 2023 配置Tomcat 8教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06

