Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

android - How to read this json data?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...