-4

How to read this json data from server

{
"DS": {
    "LST": [


     {
                "OID": 1,
                "OCD": "1",
                "OPE": "AIRCEL",
                "IPH": "Images/provider/aircelsm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 2,
                "OCD": "3",
                "OPE": "AIRTEL",
                "IPH": "Images/provider/airtelsm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 22,
                "OCD": "BSR",
                "OPE": "BSNL SPL RECHARGE",
                "IPH": "",
                "MIL": 0,
                "MXL": 0
            },
            {
                "OID": 4,
                "OCD": "4",
                "OPE": "BSNL Topup",
                "IPH": "Images/provider/bsnlsm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 6,
                "OCD": "5",
                "OPE": "DOCOMO",
                "IPH": "Images/provider/docomosm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 7,
                "OCD": "6",
                "OPE": "IDEA",
                "IPH": "Images/provider/ideasm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 8,
                "OCD": "7",
                "OPE": "MTS",
                "IPH": "Images/provider/mtssm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 5,
                "OCD": "8",
                "OPE": "RELAINCE",
                "IPH": "Images/provider/reliancesm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 3,
                "OCD": "9",
                "OPE": "VODAFONE",
                "IPH": "Images/provider/vodafonesm.jpg",
                "MIL": 10,
                "MXL": 10
            }
        ],
        "LST1": [
            {
                "OID": 10,
                "OCD": "0",
                "OPE": "AIRTEL DTH",
                "IPH": "Images/provider/airtelsm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 11,
                "OCD": "0",
                "OPE": "BIGTV",
                "IPH": "Images/provider/aircelsm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 12,
                "OCD": "0",
                "OPE": "DISH TV",
                "IPH": "Images/provider/dishtvsm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 9,
                "OCD": "0",
                "OPE": "SUN DIRECT",
                "IPH": "Images/provider/sundirectsm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 13,
                "OCD": "0",
                "OPE": "TATA SKY",
                "IPH": "Images/provider/tataskysm.jpg",
                "MIL": 10,
                "MXL": 10
            },
            {
                "OID": 14,
                "OCD": "0",
                "OPE": "VIDEOCON DTH",
                "IPH": "Images/provider/videoconsm.jpg",
                "MIL": 10,
                "MXL": 10
            }
        ]
}
}

For reading the above json type i used the method is given below

 Gson gson = new Gson();
                    Type listType = new TypeToken<List<SpinnerMenuItems>>(){}.getType();
                    List<SpinnerMenuItems> selectedNetwork = gson.fromJson(gson.toJson(result.getResult()), listType);
                    settingDropDown(selectedNetwork);

Actually my problem is to read the json array from the json object and the json array are viewed in a listview by using custom adapter. I don't know how to read this.

For reading the json object i used the following model class

public class SpinnerMenuItems {
    @SerializedName("LST")
    String zeroList;
    @SerializedName("LST1")
    String firstList;

    public String getZeroList() {
        return zeroList;
    }

    public void setZeroList(String zeroList) {
        this.zeroList = zeroList;
    }

    public String getFirstList() {
        return firstList;
    }

    public void setFirstList(String firstList) {
        this.firstList = firstList;
    }

}

The above model class is to read the list inside the json object.

The below model class is to read the json array which is placed inside the json object

public class ListZero {

    @SerializedName("IPH")
    String images;
    @SerializedName("OID")
    String oid;
    @SerializedName("OPE")
    String ope;
    @SerializedName("OCD")
    String ocd;
    @SerializedName("MIL")
    String mil;

    public String getMxl() {
        return mxl;
    }

    public void setMxl(String mxl) {
        this.mxl = mxl;
    }

    public String getMil() {
        return mil;
    }

    public void setMil(String mil) {
        this.mil = mil;
    }

    public String getOcd() {
        return ocd;
    }

    public void setOcd(String ocd) {
        this.ocd = ocd;
    }

    public String getOpe() {
        return ope;
    }

    public void setOpe(String ope) {
        this.ope = ope;
    }

    public String getOid() {
        return oid;
    }

    public void setOid(String oid) {
        this.oid = oid;
    }

    public String getImages() {
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }

    @SerializedName("MXL")
    String mxl;

}

Please help me how to read the json array which is placed inside the json object. In my case i need to read the only one json array list and view the list in a listview. How to read the json array.

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
Prabha Karan
  • 1,309
  • 3
  • 17
  • 35

4 Answers4

0

This is not correct:

@SerializedName("LST")
String zeroList;
@SerializedName("LST1")
String firstList;

see your json..

enter image description here

you dont have there Strings but a list of Objects....

those objects look almost like ListZero but you are missing the MXL attribute...


your pojo should look like

class SpinnerMenuItems {

    @SerializedName("LST")
    List<ListZero> zeroList;
    @SerializedName("LST1")
    List<ListZero> firstList;

    public List<ListZero> getZeroList() {
        return zeroList;
    }

    public void setZeroList(List<ListZero> zeroList) {
        this.zeroList = zeroList;
    }

    public List<ListZero> getFirstList() {
        return firstList;
    }

    public void setFirstList(List<ListZero> firstList) {
        this.firstList = firstList;
    }

}

and you need an extra class (the one with DS)

class DS {

    @SerializedName("DS")
    private SMI mSMI;

    public SMI getmSMI() {
        return mSMI;
    }

    public void setmSMI(SMI mSMI) {
        this.mSMI = mSMI;
    }
}

to test that place the json in a file and do:

public static void main(String[] args) throws FileNotFoundException {
    Gson gson = new Gson();
    JsonReader reader = new JsonReader(new FileReader("./foo.json"));

    DS x = gson.fromJson(reader, DS.class);
    SMI mSMI = x.getmSMI();
    System.out.println(x.toString());
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0
public class ArrayObj{
    private int OID;
    private String OCD;
    private String OPE;
    private String IPH;
    private int MIL;
    private int MXL;
    ...
    add the getters and setters
}

public class LSTObject{
    private List<ArrayObj> LST;
    ...
    add the getters and setters
}

public class FinalJSON{
    private LSTObject DS;
    ...
    add the setters and getters
}

Gson gson = new Gson();
FinalJSON finalJSON = gson.fromJson(jsonStr,FinalJSON.class);
Qian Sijianhao
  • 564
  • 7
  • 19
0

You can use the http://www.jsonschema2pojo.org/ tool to parse your Json data easily.

In your case it should be like this.

com.example.DS.java

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class DS {

@SerializedName("LST")
@Expose
private List<LST> lST = null;
@SerializedName("LST1")
@Expose
private List<LST1> lST1 = null;

public List<LST> getLST() {
return lST;
}

public void setLST(List<LST> lST) {
this.lST = lST;
}

public List<LST1> getLST1() {
return lST1;
}

public void setLST1(List<LST1> lST1) {
this.lST1 = lST1;
}

}

com.example.Example.java

package com.example;

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

public class Example {

@SerializedName("DS")
@Expose
private DS dS;

public DS getDS() {
return dS;
}

public void setDS(DS dS) {
this.dS = dS;
}

}

com.example.LST.java

package com.example;

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

public class LST {

@SerializedName("OID")
@Expose
private Integer oID;
@SerializedName("OCD")
@Expose
private String oCD;
@SerializedName("OPE")
@Expose
private String oPE;
@SerializedName("IPH")
@Expose
private String iPH;
@SerializedName("MIL")
@Expose
private Integer mIL;
@SerializedName("MXL")
@Expose
private Integer mXL;

public Integer getOID() {
return oID;
}

public void setOID(Integer oID) {
this.oID = oID;
}

public String getOCD() {
return oCD;
}

public void setOCD(String oCD) {
this.oCD = oCD;
}

public String getOPE() {
return oPE;
}

public void setOPE(String oPE) {
this.oPE = oPE;
}

public String getIPH() {
return iPH;
}

public void setIPH(String iPH) {
this.iPH = iPH;
}

public Integer getMIL() {
return mIL;
}

public void setMIL(Integer mIL) {
this.mIL = mIL;
}

public Integer getMXL() {
return mXL;
}

public void setMXL(Integer mXL) {
this.mXL = mXL;
}

}

com.example.LST1.java

package com.example;

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

public class LST1 {

@SerializedName("OID")
@Expose
private Integer oID;
@SerializedName("OCD")
@Expose
private String oCD;
@SerializedName("OPE")
@Expose
private String oPE;
@SerializedName("IPH")
@Expose
private String iPH;
@SerializedName("MIL")
@Expose
private Integer mIL;
@SerializedName("MXL")
@Expose
private Integer mXL;

public Integer getOID() {
return oID;
}

public void setOID(Integer oID) {
this.oID = oID;
}

public String getOCD() {
return oCD;
}

public void setOCD(String oCD) {
this.oCD = oCD;
}

public String getOPE() {
return oPE;
}

public void setOPE(String oPE) {
this.oPE = oPE;
}

public String getIPH() {
return iPH;
}

public void setIPH(String iPH) {
this.iPH = iPH;
}

public Integer getMIL() {
return mIL;
}

public void setMIL(Integer mIL) {
this.mIL = mIL;
}

public Integer getMXL() {
return mXL;
}

public void setMXL(Integer mXL) {
this.mXL = mXL;
}

}

After this Parse json to custom model using Gson

Gson gson = new GsonBuilder().create();
Example yourModelClass = gson.fromJson(yourJsonResponse, Example.class);

from there you can access anything which ever you want from the model. like

DS myDSObject = yourModelClass.getDS();
List<LST> lST = myDSObject.getLST();

LST myLSTObject = lST.get(0) // 1st Item. 

Then here inside array data you can get like

 String oID= myLSTObject.getOID();
 String oCD=   myLSTObject.getOCD();
 String oPE=   myLSTObject.getOPE();

...... in similar way you can get the rest data

King of Masses
  • 18,405
  • 4
  • 60
  • 77
0
    public class Data {

    @SerializedName("OID")
    String oid;
    @SerializedName("OCD")
    String ocd;
    @SerializedName("OPE")
    String ope;
    @SerializedName("IPH")
    String ocd;
    @SerializedName("MIL")
    String mil;
    @SerializedName("MXL")
    String mxl;

    // generate getter/setters
    ...

}

public class DS {

@SerializedName("LST")
private List<Data> lstObject;

@SerializedName("LST1")
private List<Data> lst1Object;

// generate getter/setters
...
}

//parsing code.

            Gson gson = new Gson();
            List<Data> lstList = new ArrayList<>(); // LST
            List<Data> lst1List = new ArrayList<>();  /// LST1
            DS ds = new DS();

            JSONObject rootObj = jsonObject.getJSONObject("DS");
            JSONArray lstArray = rootObj.getJSONArray("LST");
            JSONArray lst1Array = rootObj.getJSONArray("LST1");

            for(int i=0; i<lstArray.length; i++) {
               JSONObject obj = lstArray.getJSONObject(i);
               Data data = gson.fromJson(obj.toString(), Data.class);
               lstList.add(data);
            }

            for(int i=0; i<lst1Array.length; i++) {
               JSONObject obj = lst1Array.getJSONObject(i);
               Data data = gson.fromJson(obj.toString(), Data.class);
               lst1List.add(data);
            }

            ds.setLST(lstList);
            ds.setLST1(lst1List);
Amey Haldankar
  • 2,223
  • 1
  • 24
  • 22