如何阅读这个json数据?

时间:2017-08-16 09:44:29

标签: android json arraylist

如何从服务器读取此json数据

{
"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类型,下面给出了该方法

 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数组,并使用自定义适配器在listview中查看json数组。我不知道怎么读这个。

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

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对象中的列表。

下面的模型类是读取放置在json对象

中的json数组
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数组列表并在列表视图中查看列表。如何阅读json数组。

4 个答案:

答案 0 :(得分:0)

这是不正确的:

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

看你的json ..

enter image description here

你没有字符串,但有一个对象列表......

这些对象看起来几乎像 ListZero ,但您缺少 MXL 属性......

你的pojo应该看起来像

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

}

你需要一个额外的课程(DS的那个)

class DS {

    @SerializedName("DS")
    private SMI mSMI;

    public SMI getmSMI() {
        return mSMI;
    }

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

测试将json放在文件中并执行:

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

答案 1 :(得分: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);

答案 2 :(得分:0)

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

在你的情况下它应该是这样的。

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

}

使用Gson将此Parse json转换为自定义模型后

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

从那里你可以从模型中访问任何你想要的东西。像

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

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

然后在数组数据中你可以得到像

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

......以类似的方式,您可以获得其余数据

答案 3 :(得分: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);