我在从json对象
读取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数据来自服务器以及如何读取json数组。
我的用于读取json数据的模型类是
public class SpinnerRootObject {
@SerializedName("DS")
SpinnerMenuItems ds;
public SpinnerMenuItems getDs() {
return ds;
}
public void setDs(SpinnerMenuItems ds) {
this.ds = ds;
}
}
public class SpinnerMenuItems
{
@SerializedName("LST")
ListZero zeroList;
@SerializedName("LST1")
String firstList;
public ListZero getZeroList() {
return zeroList;
}
public void setZeroList( ListZero zeroList) {
this.zeroList = zeroList;
}
public String getFirstList() {
return firstList;
}
public void setFirstList(String firstList) {
this.firstList = firstList;
}
}
public class ListZero {
@SerializedName("IPH")
String images;
@SerializedName("OID")
String oid;
@SerializedName("OPE")
String ope;
@SerializedName("OCD")
String ocd;
@SerializedName("MIL")
String mil;
@SerializedName("MXL")
String mxl;
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;
}
}
上面的代码是用于阅读json的模型类。
用于从服务器读取json的代码是
Gson gson = new Gson();
SpinnerRootObject spinnerRootObject = gson.fromJson(result.getResult(), SpinnerRootObject.class);
settingDropDown((List<SpinnerRootObject>) spinnerRootObject);
使用上述方法时会发生异常 引起:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第15列路径处是BEGIN_ARRAY。我不知道如何在给定的json数据中读取json数组
我需要将json arraylist数据传递给自定义适配器
public void settingDropDown( List<SpinnerRootObject> selectedNetwork)
{
customAdapter = new Fragment_DTH_Main_Spinner_Adapter
(getActivity(),R.layout.fragment_dth_main_spinner_items,R.id.serviceName,selectedNetwork);
selectMenu.setAdapter(customAdapter);
}
请帮助我如何从json对象
获取json数组答案 0 :(得分:0)
有些数据是整数,所以需要更改此类
public class ListZero {
@SerializedName("IPH")
String images;
@SerializedName("OID")
int oid; // do this integer
@SerializedName("OPE")
String ope;
@SerializedName("OCD")
String ocd;
@SerializedName("MIL")
int mil; // do this integer
@SerializedName("MXL")
int mxl; // do this integer
//getter setter ... do modify getter and setter of oid,mil and mxl accordingly..
}
答案 1 :(得分:0)
import com.google.gson.annotations.SerializedName;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class SpinnerRootObject {
@SerializedName("DS")
@Expose
private DS dS;
public DS getDS() {
return dS;
}
public void setDS(DS dS) {
this.dS = dS;
}
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;
}
}
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;
}
}
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;
}
}
}
Try this model class.