" />

亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java如何對(duì)返回參數(shù)進(jìn)行處理

 更新時(shí)間:2024年07月05日 09:04:49   作者:HelIanthuss  
這篇文章主要介紹了Java如何對(duì)返回參數(shù)進(jìn)行處理問(wè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":"客戶(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)文章

  • Java中的System.getProperty()詳解

    Java中的System.getProperty()詳解

    System.getProperty("XXX")方法用來(lái)讀取JVM中的系統(tǒng)屬性,那么java 虛擬機(jī)中的系統(tǒng)屬性使用在運(yùn)行java程序的時(shí)候java -D配置,有兩種方式,一種是在命令行配置另一種是在IDE中配置,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2023-09-09
  • SpringBoot整合ELK實(shí)現(xiàn)日志監(jiān)控

    SpringBoot整合ELK實(shí)現(xiàn)日志監(jiān)控

    這篇文章主要為大家詳細(xì)介紹了SpringBoot整合ELK實(shí)現(xiàn)日志監(jiān)控的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-11-11
  • java 文件流的處理方式 文件打包成zip

    java 文件流的處理方式 文件打包成zip

    這篇文章主要介紹了java 文件流的處理方式 文件打包成zip,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java根據(jù)當(dāng)前時(shí)間獲取yyyy-MM-dd?HH:mm:ss標(biāo)準(zhǔn)格式的時(shí)間代碼示例

    java根據(jù)當(dāng)前時(shí)間獲取yyyy-MM-dd?HH:mm:ss標(biāo)準(zhǔn)格式的時(shí)間代碼示例

    在Java中可以使用java.time包中的LocalDateTime類(lèi)和DateTimeFormatter類(lèi)來(lái)獲取并格式化當(dāng)前時(shí)間為yyyy-MM-dd?HH:mm:ss的格式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-10-10
  • Spring中bean標(biāo)簽的用法詳解

    Spring中bean標(biāo)簽的用法詳解

    Bean標(biāo)簽一般用于配置對(duì)象交由Spring?來(lái)創(chuàng)建,這篇文章主要來(lái)和大家詳細(xì)聊聊Spring中bean標(biāo)簽的用法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • String.trim()消除不了空格的問(wèn)題及解決

    String.trim()消除不了空格的問(wèn)題及解決

    這篇文章主要介紹了String.trim()消除不了空格的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 淺談Spring Boot 開(kāi)發(fā)REST接口最佳實(shí)踐

    淺談Spring Boot 開(kāi)發(fā)REST接口最佳實(shí)踐

    這篇文章主要介紹了淺談Spring Boot 開(kāi)發(fā)REST接口最佳實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • 詳解Java分布式系統(tǒng)中一致性哈希算法

    詳解Java分布式系統(tǒng)中一致性哈希算法

    這篇文章主要介紹了Java分布式系統(tǒng)中一致性哈希算法,對(duì)算法感興趣的同學(xué),可以參考下
    2021-04-04
  • Springboot中Jackson用法詳解

    Springboot中Jackson用法詳解

    Springboot自帶默認(rèn)json解析Jackson,可以在不引入其他json解析包情況下,解析json字段,下面我們就來(lái)聊聊Springboot中Jackson的用法吧
    2025-01-01
  • SpringBoot實(shí)現(xiàn)快遞物流查詢(xún)功能(快遞鳥(niǎo))

    SpringBoot實(shí)現(xiàn)快遞物流查詢(xún)功能(快遞鳥(niǎo))

    本文將基于springboot2.4.0實(shí)現(xiàn)快遞物流查詢(xún),物流信息的獲取通過(guò)快遞鳥(niǎo)第三方實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-10-10

最新評(píng)論