-1

I have in a rest response this json:

{
      "TRANS": {
        "HPAY": [
          {
            "ID": "1234",
            "DATE": "10/09/2011 18:09:27",
            "REC": "wallet Ricaricato",
            "COM": "Tasso Commissione",
            "MSG": "Commento White Brand",
            "STATUS": "3",
            "EXTRA": {
              "IS3DS": "0",
              "CTRY": "FRA",
              "AUTH": "455622"
            },
            "INT_MSG": "05-00-05 ERR_PSP_REFUSED",
            "MLABEL": "IBAN",
            "TYPE": "1"
          }
        ]
      }
    }

I have made pojo class to map this json in java.

public class Trans {

    private List<Hpay> hpay;

    public Trans(){

    }
//getter and setter
}

public class Hpay {

    private String id;
    private String date;
    private String com;
    private String msg;
    private String status;
    private List<Extra> extra;
    private String int_msg;
    private String mlabel;
    private String type;

    public Hpay(){

    }
//getter and setter
}

I try to map the object with Gson library.

Gson gson=new Gson();
Trans transaction=gson.fromJson(response.toString(), Trans.class);

If i call hpay method on transaction i have null..i don't know why...

Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
Filomena
  • 29
  • 1
  • 9

2 Answers2

1

I have deleted previous answer and add new one as par your requirement

JSON String :

{
    "TRANS": {
        "HPAY": [{
                "ID": "1234",
                "DATE": "10/09/2011 18:09:27",
                "REC": "wallet Ricaricato",
                "COM": "Tasso Commissione",
                "MSG": "Commento White Brand",
                "STATUS": "3",
                "EXTRA": {
                    "IS3DS": "0",
                    "CTRY": "FRA",
                    "AUTH": "455622"
                },
                "INT_MSG": "05-00-05 ERR_PSP_REFUSED",
                "MLABEL": "IBAN",
                "TYPE": "1"
            }
        ]
    }
}

Java Objects : (Here Extra is not list)

public class MyObject {
    @SerializedName("TRANS")
    @Expose
    private Trans trans;

    public Trans getTRANS() {return trans;}

    public void setTRANS(Trans trans) {this.trans = trans;}
}

public class Trans {
    @SerializedName("HPAY")
    @Expose
    private List<HPay> hPay;

    public List<HPay> getHPAY() {return hPay;}

    public void setHPAY(List<HPay> hPay) {this.hPay = hPay;}
}

public class HPay {
    @SerializedName("ID")
    @Expose
    private String id;
    @SerializedName("DATE")
    @Expose
    private String date;
    @SerializedName("REC")
    @Expose
    private String rec;
    @SerializedName("COM")
    @Expose
    private String com;
    @SerializedName("MSG")
    @Expose
    private String msg;
    @SerializedName("STATUS")
    @Expose
    private String status;
    @SerializedName("EXTRA")
    @Expose
    private Extra extra;
    @SerializedName("INT_MSG")
    @Expose
    private String intMsg;
    @SerializedName("MLABEL")
    @Expose
    private String mLabel;
    @SerializedName("TYPE")
    @Expose
    private String type;

    public String getID() {return id;}
    public void setID(String id) {this.id = id;}
    public String getDATE() {return date;}
    public void setDATE(String date) {this.date = date;}
    public String getREC() {return rec;}
    public void setREC(String rec) {this.rec = rec;}
    public String getCOM() {return com;}
    public void setCOM(String com) {this.com = com;}
    public String getMSG() {return msg;}
    public void setMSG(String msg) {this.msg = msg;}
    public String getSTATUS() {return status;}
    public void setSTATUS(String status) {this.status = status;}
    public Extra getEXTRA() {return extra;}
    public void setEXTRA(Extra extra) {this.extra = extra;}
    public String getINTMSG() {return intMsg;}
    public void setINTMSG(String intMsg) {this.intMsg = intMsg;}
    public String getMLABEL() {return mLabel;}
    public void setMLABEL(String mLabel) {this.mLabel = mLabel;}
    public String getTYPE() {return type;}
    public void setTYPE(String type) {this.type = type;}
}

public class Extra {
    @SerializedName("IS3DS")
    @Expose
    private String is3ds;
    @SerializedName("CTRY")
    @Expose
    private String ctry;
    @SerializedName("AUTH")
    @Expose
    private String auth;

    public String getIS3DS() { return is3ds; }
    public void setIS3DS(String is3ds) { this.is3ds = is3ds; }
    public String getCTRY() { return ctry; }
    public void setCTRY(String ctry) { this.ctry = ctry; }
    public String getAUTH() { return auth; }
    public void setAUTH(String auth) { this.auth = auth; }
}

Conversion Logic :

import com.google.gson.Gson;
    public class NewClass {
    public static void main(String[] args) {
        Gson g = new Gson();

        g.fromJson(json, MyObject.class);

    }

    static String json = "{ \"TRANS\": { \"HPAY\": [{ \"ID\": \"1234\", \"DATE\": \"10/09/2011 18:09:27\", \"REC\": \"wallet Ricaricato\", \"COM\": \"Tasso Commissione\", \"MSG\": \"Commento White Brand\", \"STATUS\": \"3\", \"EXTRA\": { \"IS3DS\": \"0\", \"CTRY\": \"FRA\", \"AUTH\": \"455622\" }, \"INT_MSG\": \"05-00-05 ERR_PSP_REFUSED\", \"MLABEL\": \"IBAN\", \"TYPE\": \"1\" } ] } }";
}

Here I use Google Gosn lib for conversion.

And need to import bellow classes for annotation

com.google.gson.annotations.Expose; com.google.gson.annotations.SerializedName;

Sunil Kanzar
  • 1,244
  • 1
  • 9
  • 21
  • You most welcome... In case of any query late me know. – Sunil Kanzar May 11 '17 at 11:14
  • i have to change my get/set method or not? – Filomena May 11 '17 at 11:16
  • It does't matter what set/get method name is. it works on annotation. if your json field is get change then you need to change the test inside `SerializedName` Like: `@SerializedName("")` – Sunil Kanzar May 11 '17 at 11:21
  • Now i have the same problem with another json.. which structure is here: https://demo2855016.mockable.io/GetKycStatus I have used the same structure but not works :( – Filomena May 11 '17 at 15:06
0

First parse the json data using json.simple and then set the values using setters

 Object obj = parser.parse(new FileReader( "file.json" ));
   JSONObject jsonObject = (JSONObject) obj;

   JSONArray hpayObj= (JSONArray) jsonObject.get("HPAY");

    //get the first element of array
    JSONObject details= hpayObj.getJSONArray(0);

    String id = (String)details.get("ID");
        //set the value of the id field in the setter of class Trans
       new Trans().setId(id);
       new Trans().setDate((String)details.get("DATE"));
       new Trans().setRec((String)details.get("REC"));

and so on..

 //get the second array element
      JSONObject intMsgObj= hpayObj.getJSONArray(1);
      new Trans().setIntmsg((String)details.get("INT_MSG"));

  //get the third array element
   JSONObject mlabelObj= hpayObj.getJSONArray(2);
      new Trans().setMlabel((String)details.get("MLABEL"));

JSONObject typeObj= hpayObj.getJSONArray(3);
 new Trans().setType((String)details.get("TYPE"));

Now you can get the values using your getter methods.

Vishesh
  • 308
  • 1
  • 9