如何从JAX-RS获取JSON响应的特定值?

时间:2018-07-19 06:59:58

标签: java jax-rs jersey-client jersey-1.0

我正在将JAX-RS与jersey 1.6配合使用以使用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,令牌,平移一些单独的变量。我该如何实现?

1 个答案:

答案 0 :(得分:1)

你在这里 1- Json抽象课

const joborderlist =  [
        {
            "id": "5b4f7ad860dfee3b009d7452",
            "haulier": {
                "companyName": "BigDataMatica",
                "email": "nirmalkumar.s@datinfi.com",
                "registrationNumber": "nirmal89HJ",
                "companyAddress": "RSPURAM",
                "companyPhone": "8687678",
                "yardAddress": "Pragatinagar",
                "yardPhone": "69876876",
                "haulierCode": "Haulier",
                "billingAddress": "Pragatinagar"
            }
        },
        {
            "id": "5b501f8f60dfee3b009d7454",
            "haulier": {
                "companyName": "BigDataMatica",
                "email": "nirmalkumar.s@datinfi.com",
                "registrationNumber": "nirmal89HJ",
                "companyAddress": "RS PURAM",
                "companyPhone": "8687678",
                "yardAddress": "Pragatinagar",
                "yardPhone": "69876876",
                "haulierCode": "Haulier",
                "billingAddress": "Pragatinagar"
            }
        },
        {
            "id": "5b5020f360dfee3b009d7455",
            "haulier": {
                "companyName": "BigDataMatica",
                "email": "sivasai.s@datinfi.com",
                "registrationNumber": "nirmal89HJ",
                "companyAddress": "RS PURAM",
                "companyPhone": "8687678",
                "yardAddress": "Pragatinagar",
                "yardPhone": "69876876",
                "haulierCode": "Haulier",
                "billingAddress": "Pragatinagar"
            }
        }
    ]

const haulierjobordersnames = 'sivasai.s@datinfi.com';
let haulierjoborders = joborderlist && joborderlist.filter && joborderlist.filter((el)=>{ return el.haulier.email == haulierjobordersnames})
console.log("haulierjoborders", haulierjoborders);

2-细节课程

   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());
    }
}

3- TestDTO类

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;
     }
}

4-运行TestDTO类,这是输出

  

令牌:xxxxxxxxxxxxxxxxxxxx PAN:500127108 EBP编号:4977

只需在代码中保留空值,然后导入gson-2.7.jar