如何从 JAX-RS 获取 JSON 响应的具体值?

how to get the specific value of JSON response from JAX-RS ?

我正在使用带有 jersey 1.6 的 JAX-RS 来使用 API。到目前为止,我已经能够使用它,但现在我只需要一些特定的值。

我的消费方式api:-

ClientResponse ebpResonse = ebpResource.type("application/json")
        .header(HttpHeaders.AUTHORIZATION, token)
           .post(ClientResponse.class, ebpReq1);



System.out.println("ebp response is: " + ebpResonse.getEntity(String.class));

我收到如下回复:-

{
    "code": "2075-4673",
    "data": {
        "requestId": 4673,
        "requestCode": "2075-4673",
        "fiscalYear": {
            "fiscalYearId": 2075,
            "fiscalYearCode": "2075/76"
        },
        "requestDate": 1531851300000,
        "requestNdate": "2075/04/02",
        "rcAgency": {
            "id": 2373,
            "code": "210003501",
            "rcAgencyEDesc": "ABC",
            "rcAgencyNDesc": " सेवा ",
            "nepaliName": " सेवा",
            "engishName": null
        },
        "status": 1,
        "pan": "500127108",
        "payerCode": null,
        "payerEdesc": "ABC Enterprises",
        "payerNdesc": null,
        "payerAddress": null,
        "payerPhone": null,
        "totalAmount": 14000,
        "createdBy": "psctest",
        "createdOn": 1531851300000,
        "collectedBank": null,
        "collectedDate": null,
        "collectedBy": null,
        "token": "xxxxxxxxxxxxxxxx",
        "details": [
            {
                "ebpNo": "4977",
                "sno": 1,
                "depositSlipNo": null,
                "purpose": null,
                "revenueHead": {
                    "id": 14224,
                    "code": "14224",
                    "oldCode": "14224",
                    "nepaliName": "शुल्क",
                    "englishName": "शुल्क",
                    "description": "शुल्क",
                    "nepaliDescription": "शुल्क",
                    "preRevenueHeadId": 0,
                    "status": true,
                    "federal": true,
                    "state": true,
                    "local": true,
                    "remarks": "xxxxx"
                },
                "remarks": "remarks",
                "description": "Production",
                "currency": {
                    "currencyId": 524,
                    "currencyCode": "524",
                    "descEnglish": "NRS",
                    "descNepali": "NRS"
                },
                "amount": 14000,
                "taxAdv": false,
                "taxyearId": 2074,
                "dueAmount": 14000,
                "createdBy": "psctest",
                "createdOn": 1531894162000
            }
        ]
    },
    "message": "Voucher has saved sucessfully.",
    "token": "xxxxxxxxxxxxxxxxxxxx",
    "status": 0
}

这个回复对我来说也包含了太多不必要的信息。我需要在一些单独的变量中获取 epbNo、token、pan。我怎样才能实现它?

给你 1- Json 摘要 class

   package test;
import com.google.gson.Gson;
public abstract class JsonObject {
    @Override
    public String toString() {
          Gson gson = new Gson();
          return gson.toJson(this);
    }
    public Object toObject(String json){
        Gson gson = new Gson();
        return gson.fromJson(json, this.getClass());
    }
}

2- 详情 class

package  test;

public class Detail extends JsonObject {
     private String ebpNo;
     private float sno;
     private String depositSlipNo = null;
     private String purpose = null;
     RevenueHead RevenueHeadObject;
     private String remarks;
     private String description;
     Currency CurrencyObject;
     private float amount;
     private boolean taxAdv;
     private float taxyearId;
     private float dueAmount;
     private String createdBy;
     private float createdOn;


     // Getter Methods 

     public String getEbpNo() {
      return ebpNo;
     }

     public float getSno() {
      return sno;
     }

     public String getDepositSlipNo() {
      return depositSlipNo;
     }

     public String getPurpose() {
      return purpose;
     }

     public RevenueHead getRevenueHead() {
      return RevenueHeadObject;
     }

     public String getRemarks() {
      return remarks;
     }

     public String getDescription() {
      return description;
     }

     public Currency getCurrency() {
      return CurrencyObject;
     }

     public float getAmount() {
      return amount;
     }

     public boolean getTaxAdv() {
      return taxAdv;
     }

     public float getTaxyearId() {
      return taxyearId;
     }

     public float getDueAmount() {
      return dueAmount;
     }

     public String getCreatedBy() {
      return createdBy;
     }

     public float getCreatedOn() {
      return createdOn;
     }

     // Setter Methods 

     public void setEbpNo(String ebpNo) {
      this.ebpNo = ebpNo;
     }

     public void setSno(float sno) {
      this.sno = sno;
     }

     public void setDepositSlipNo(String depositSlipNo) {
      this.depositSlipNo = depositSlipNo;
     }

     public void setPurpose(String purpose) {
      this.purpose = purpose;
     }

     public void setRevenueHead(RevenueHead revenueHeadObject) {
      this.RevenueHeadObject = revenueHeadObject;
     }

     public void setRemarks(String remarks) {
      this.remarks = remarks;
     }

     public void setDescription(String description) {
      this.description = description;
     }

     public void setCurrency(Currency currencyObject) {
      this.CurrencyObject = currencyObject;
     }

     public void setAmount(float amount) {
      this.amount = amount;
     }

     public void setTaxAdv(boolean taxAdv) {
      this.taxAdv = taxAdv;
     }

     public void setTaxyearId(float taxyearId) {
      this.taxyearId = taxyearId;
     }

     public void setDueAmount(float dueAmount) {
      this.dueAmount = dueAmount;
     }

     public void setCreatedBy(String createdBy) {
      this.createdBy = createdBy;
     }

     public void setCreatedOn(float createdOn) {
      this.createdOn = createdOn;
     }
    }
 class Currency extends JsonObject {
     private float currencyId;
     private String currencyCode;
     private String descEnglish;
     private String descNepali;


     // Getter Methods 

     public float getCurrencyId() {
      return currencyId;
     }

     public String getCurrencyCode() {
      return currencyCode;
     }

     public String getDescEnglish() {
      return descEnglish;
     }

     public String getDescNepali() {
      return descNepali;
     }

     // Setter Methods 

     public void setCurrencyId(float currencyId) {
      this.currencyId = currencyId;
     }

     public void setCurrencyCode(String currencyCode) {
      this.currencyCode = currencyCode;
     }

     public void setDescEnglish(String descEnglish) {
      this.descEnglish = descEnglish;
     }

     public void setDescNepali(String descNepali) {
      this.descNepali = descNepali;
     }
    }
 class RevenueHead extends JsonObject {
     private float id;
     private String code;
     private String oldCode;
     private String nepaliName;
     private String englishName;
     private String description;
     private String nepaliDescription;
     private float preRevenueHeadId;
     private boolean status;
     private boolean federal;
     private boolean state;
     private boolean local;
     private String remarks;


     // Getter Methods 

     public float getId() {
      return id;
     }

     public String getCode() {
      return code;
     }

     public String getOldCode() {
      return oldCode;
     }

     public String getNepaliName() {
      return nepaliName;
     }

     public String getEnglishName() {
      return englishName;
     }

     public String getDescription() {
      return description;
     }

     public String getNepaliDescription() {
      return nepaliDescription;
     }

     public float getPreRevenueHeadId() {
      return preRevenueHeadId;
     }

     public boolean getStatus() {
      return status;
     }

     public boolean getFederal() {
      return federal;
     }

     public boolean getState() {
      return state;
     }

     public boolean getLocal() {
      return local;
     }

     public String getRemarks() {
      return remarks;
     }

     // Setter Methods 

     public void setId(float id) {
      this.id = id;
     }

     public void setCode(String code) {
      this.code = code;
     }

     public void setOldCode(String oldCode) {
      this.oldCode = oldCode;
     }

     public void setNepaliName(String nepaliName) {
      this.nepaliName = nepaliName;
     }

     public void setEnglishName(String englishName) {
      this.englishName = englishName;
     }

     public void setDescription(String description) {
      this.description = description;
     }

     public void setNepaliDescription(String nepaliDescription) {
      this.nepaliDescription = nepaliDescription;
     }

     public void setPreRevenueHeadId(float preRevenueHeadId) {
      this.preRevenueHeadId = preRevenueHeadId;
     }

     public void setStatus(boolean status) {
      this.status = status;
     }

     public void setFederal(boolean federal) {
      this.federal = federal;
     }

     public void setState(boolean state) {
      this.state = state;
     }

     public void setLocal(boolean local) {
      this.local = local;
     }

     public void setRemarks(String remarks) {
      this.remarks = remarks;
     }
}

3- TestDTO class

/**
 * 
 */
package test;

import java.util.ArrayList;

/**
 * @author 00990
 *
 */
public class TestDTO extends JsonObject {
    private String code;
    Data data;
    private String message;
    private String token;
    private float status;

    // Getter Methods

    public String getCode() {
        return code;
    }

    public Data getData() {
        return data;
    }

    public String getMessage() {
        return message;
    }

    public String getToken() {
        return token;
    }

    public float getStatus() {
        return status;
    }

    // Setter Methods

    public void setCode(String code) {
        this.code = code;
    }

    public void setData(Data dataObject) {
        this.data = dataObject;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setStatus(float status) {
        this.status = status;
    }


    public static void main(String[] args) {
        String jsonObj = "{\r\n" + "    \"code\": \"2075-4673\",\r\n" + "    \"data\": {\r\n"
                + "        \"requestId\": 4673,\r\n" + "        \"requestCode\": \"2075-4673\",\r\n"
                + "        \"fiscalYear\": {\r\n" + "            \"fiscalYearId\": 2075,\r\n"
                + "            \"fiscalYearCode\": \"2075/76\"\r\n" + "        },\r\n"
                + "        \"requestDate\": 1531851300000,\r\n" + "        \"requestNdate\": \"2075/04/02\",\r\n"
                + "        \"rcAgency\": {\r\n" + "            \"id\": 2373,\r\n"
                + "            \"code\": \"210003501\",\r\n" + "            \"rcAgencyEDesc\": \"ABC\",\r\n"
                + "            \"rcAgencyNDesc\": \" सेवा \",\r\n" + "            \"nepaliName\": \" सेवा\",\r\n"
                + "            \"engishName\": null\r\n" + "        },\r\n" + "        \"status\": 1,\r\n"
                + "        \"pan\": \"500127108\",\r\n" + "        \"payerCode\": null,\r\n"
                + "        \"payerEdesc\": \"ABC Enterprises\",\r\n" + "        \"payerNdesc\": null,\r\n"
                + "        \"payerAddress\": null,\r\n" + "        \"payerPhone\": null,\r\n"
                + "        \"totalAmount\": 14000,\r\n" + "        \"createdBy\": \"psctest\",\r\n"
                + "        \"createdOn\": 1531851300000,\r\n" + "        \"collectedBank\": null,\r\n"
                + "        \"collectedDate\": null,\r\n" + "        \"collectedBy\": null,\r\n"
                + "        \"token\": \"xxxxxxxxxxxxxxxx\",\r\n" + "        \"details\": [\r\n" + "            {\r\n"
                + "                \"ebpNo\": \"4977\",\r\n" + "                \"sno\": 1,\r\n"
                + "                \"depositSlipNo\": null,\r\n" + "                \"purpose\": null,\r\n"
                + "                \"revenueHead\": {\r\n" + "                    \"id\": 14224,\r\n"
                + "                    \"code\": \"14224\",\r\n" + "                    \"oldCode\": \"14224\",\r\n"
                + "                    \"nepaliName\": \"शुल्क\",\r\n"
                + "                    \"englishName\": \"शुल्क\",\r\n"
                + "                    \"description\": \"शुल्क\",\r\n"
                + "                    \"nepaliDescription\": \"शुल्क\",\r\n"
                + "                    \"preRevenueHeadId\": 0,\r\n" + "                    \"status\": true,\r\n"
                + "                    \"federal\": true,\r\n" + "                    \"state\": true,\r\n"
                + "                    \"local\": true,\r\n" + "                    \"remarks\": \"xxxxx\"\r\n"
                + "                },\r\n" + "                \"remarks\": \"remarks\",\r\n"
                + "                \"description\": \"Production\",\r\n" + "                \"currency\": {\r\n"
                + "                    \"currencyId\": 524,\r\n" + "                    \"currencyCode\": \"524\",\r\n"
                + "                    \"descEnglish\": \"NRS\",\r\n"
                + "                    \"descNepali\": \"NRS\"\r\n" + "                },\r\n"
                + "                \"amount\": 14000,\r\n" + "                \"taxAdv\": false,\r\n"
                + "                \"taxyearId\": 2074,\r\n" + "                \"dueAmount\": 14000,\r\n"
                + "                \"createdBy\": \"psctest\",\r\n" + "                \"createdOn\": 1531894162000\r\n"
                + "            }\r\n" + "        ]\r\n" + "    },\r\n"
                + "    \"message\": \"Voucher has saved sucessfully.\",\r\n"
                + "    \"token\": \"xxxxxxxxxxxxxxxxxxxx\",\r\n" + "    \"status\": 0\r\n" + "}";
        System.out.println("Token :" +((TestDTO) new TestDTO().toObject(jsonObj)).getToken());
        System.out.println(" PAN  :" +((TestDTO) new TestDTO().toObject(jsonObj)).getData().getPan());
        System.out.println("EBPNo :" +((TestDTO) new TestDTO().toObject(jsonObj)).getData().getDetails().get(0).getEbpNo());
    }
}

class Data extends JsonObject {
    private float requestId;
    private String requestCode;
    FiscalYear FiscalYearObject;
    private float requestDate;
    private String requestNdate;
    RcAgency RcAgencyObject;
    private float status;
    private String pan;
    private String payerCode = null;
    private String payerEdesc;
    private String payerNdesc = null;
    private String payerAddress = null;
    private String payerPhone = null;
    private float totalAmount;
    private String createdBy;
    private float createdOn;
    private String collectedBank = null;
    private String collectedDate = null;
    private String collectedBy = null;
    private String token;
    ArrayList<Detail> details = new ArrayList<Detail>();

    // Getter Methods

    public float getRequestId() {
        return requestId;
    }

    public ArrayList<Detail> getDetails() {
        return details;
    }

    public void addToDetails(Detail detail) {
        if (details == null) {
            details = new ArrayList<Detail>();
        }
        this.details.add(detail);
    }

    public String getRequestCode() {
        return requestCode;
    }

    public FiscalYear getFiscalYear() {
        return FiscalYearObject;
    }

    public float getRequestDate() {
        return requestDate;
    }

    public String getRequestNdate() {
        return requestNdate;
    }

    public RcAgency getRcAgency() {
        return RcAgencyObject;
    }

    public float getStatus() {
        return status;
    }

    public String getPan() {
        return pan;
    }

    public String getPayerCode() {
        return payerCode;
    }

    public String getPayerEdesc() {
        return payerEdesc;
    }

    public String getPayerNdesc() {
        return payerNdesc;
    }

    public String getPayerAddress() {
        return payerAddress;
    }

    public String getPayerPhone() {
        return payerPhone;
    }

    public float getTotalAmount() {
        return totalAmount;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public float getCreatedOn() {
        return createdOn;
    }

    public String getCollectedBank() {
        return collectedBank;
    }

    public String getCollectedDate() {
        return collectedDate;
    }

    public String getCollectedBy() {
        return collectedBy;
    }

    public String getToken() {
        return token;
    }

    // Setter Methods

    public void setRequestId(float requestId) {
        this.requestId = requestId;
    }

    public void setRequestCode(String requestCode) {
        this.requestCode = requestCode;
    }

    public void setFiscalYear(FiscalYear fiscalYearObject) {
        this.FiscalYearObject = fiscalYearObject;
    }

    public void setRequestDate(float requestDate) {
        this.requestDate = requestDate;
    }

    public void setRequestNdate(String requestNdate) {
        this.requestNdate = requestNdate;
    }

    public void setRcAgency(RcAgency rcAgencyObject) {
        this.RcAgencyObject = rcAgencyObject;
    }

    public void setStatus(float status) {
        this.status = status;
    }

    public void setPan(String pan) {
        this.pan = pan;
    }

    public void setPayerCode(String payerCode) {
        this.payerCode = payerCode;
    }

    public void setPayerEdesc(String payerEdesc) {
        this.payerEdesc = payerEdesc;
    }

    public void setPayerNdesc(String payerNdesc) {
        this.payerNdesc = payerNdesc;
    }

    public void setPayerAddress(String payerAddress) {
        this.payerAddress = payerAddress;
    }

    public void setPayerPhone(String payerPhone) {
        this.payerPhone = payerPhone;
    }

    public void setTotalAmount(float totalAmount) {
        this.totalAmount = totalAmount;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public void setCreatedOn(float createdOn) {
        this.createdOn = createdOn;
    }

    public void setCollectedBank(String collectedBank) {
        this.collectedBank = collectedBank;
    }

    public void setCollectedDate(String collectedDate) {
        this.collectedDate = collectedDate;
    }

    public void setCollectedBy(String collectedBy) {
        this.collectedBy = collectedBy;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

class RcAgency extends JsonObject {
    private float id;
    private String code;
    private String rcAgencyEDesc;
    private String rcAgencyNDesc;
    private String nepaliName;
    private String engishName = null;

    // Getter Methods

    public float getId() {
        return id;
    }

    public String getCode() {
        return code;
    }

    public String getRcAgencyEDesc() {
        return rcAgencyEDesc;
    }

    public String getRcAgencyNDesc() {
        return rcAgencyNDesc;
    }

    public String getNepaliName() {
        return nepaliName;
    }

    public String getEngishName() {
        return engishName;
    }

    // Setter Methods

    public void setId(float id) {
        this.id = id;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setRcAgencyEDesc(String rcAgencyEDesc) {
        this.rcAgencyEDesc = rcAgencyEDesc;
    }

    public void setRcAgencyNDesc(String rcAgencyNDesc) {
        this.rcAgencyNDesc = rcAgencyNDesc;
    }

    public void setNepaliName(String nepaliName) {
        this.nepaliName = nepaliName;
    }

    public void setEngishName(String engishName) {
        this.engishName = engishName;
    }
}

class FiscalYear extends JsonObject {
    private float fiscalYearId;
    private String fiscalYearCode;

    // Getter Methods

    public float getFiscalYearId() {
        return fiscalYearId;
    }

    public String getFiscalYearCode() {
        return fiscalYearCode;
    }

    // Setter Methods

    public void setFiscalYearId(float fiscalYearId) {
        this.fiscalYearId = fiscalYearId;
    }

    public void setFiscalYearCode(String fiscalYearCode) {
        this.fiscalYearCode = fiscalYearCode;
    }


}

4- 运行 TestDTO class,这里是输出

Token :xxxxxxxxxxxxxxxxxxxx PAN :500127108 EBPNo :4977

只需在代码中保留空值,并导入 gson-2。7.jar