如何读取此json数据? [英] How to read this json data?

查看:198
本文介绍了如何读取此json数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从服务器读取json数据

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

要读取我使用的上述json类型,方法如下所示

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

实际上,我的问题是从json对象读取json数组,并且使用自定义适配器在列表视图中查看了json数组.我不知道该怎么读.

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.

为了读取json对象,我使用了以下模型类

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

}

上面的模型类是读取json对象中的列表.

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

下面的模型类用于读取放置在json对象内部的json数组

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;

}

请帮助我如何读取放置在json对象内部的json数组.在我的情况下,我需要读取唯一的一个json数组列表,并在listview中查看该列表.如何读取json数组.

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.

推荐答案

您可以使用 http://www. jsonschema2pojo.org/工具来轻松解析您的Json数据.

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

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

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

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

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

}

使用Gson将此json解析为自定义模型后

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

这篇关于如何读取此json数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆