开发者社区> 问答> 正文

Android解析JSON数组,如果里面有两个数组,要怎么处理

项目中遇到服务器给的文档里面有两个数组
这样

{
 "code":200,
 "message":"调用成功",
 "data":{
 "mem_message_records":[
 {
 "message_disease_typeid":"1",
 "message_disease_type":"心血管系统"
 },
 {
 "message_disease_typeid":"2",
 "message_disease_type":"外围血管疾病"
 },
 {
 "message_disease_typeid":"3",
 "message_disease_type":"脑血管疾病"
 },
 {
 "message_disease_typeid":"4",
 "message_disease_type":"肺部疾病"
 },
 {
 "message_disease_typeid":"5",
 "message_disease_type":"代谢疾病"
 },
 {
 "message_disease_typeid":"6",
 "message_disease_type":"周围神经疾病"
 },
 {
 "message_disease_typeid":"7",
 "message_disease_type":"其他疾病"
 }
 ],
 "mem_message_recordss":[
 [
 {
 "message_diseaseid":"1",
 "message_disease":"冠状动脉疾病"
 },
 {
 "message_diseaseid":"2",
 "message_disease":"心绞痛"
 },
 {
 "message_diseaseid":"3",
 "message_disease":"心肌梗死"
 },
 {
 "message_diseaseid":"4",
 "message_disease":"冠状动脉血运重建"
 },
 {
 "message_diseaseid":"5",
 "message_disease":"充血性心力衰竭"
 }
 ],
 [
 {
 "message_diseaseid":"6",
 "message_disease":"闭塞性周围动脉粥样硬化"
 },
 {
 "message_diseaseid":"7",
 "message_disease":"血栓行静脉炎"
 }
 ],
 [
 {
 "message_diseaseid":"8",
 "message_disease":"脑血栓"
 },
 {
 "message_diseaseid":"9",
 "message_disease":"短暂性脑供血不足"
 },
 {
 "message_diseaseid":"10",
 "message_disease":"脑出血"
 }
 ],
 [
 {
 "message_diseaseid":"11",
 "message_disease":"慢性支气管炎"
 },
 {
 "message_diseaseid":"12",
 "message_disease":"慢性阻塞性肺病"
 },
 {
 "message_diseaseid":"13",
 "message_disease":"支气管哮喘"
 },
 {
 "message_diseaseid":"14",
 "message_disease":"间质性肺病"
 },
 {
 "message_diseaseid":"15",
 "message_disease":"囊性肺纤维化"
 }
 ],
 [
 {
 "message_diseaseid":"16",
 "message_disease":"1型糖尿病"
 },
 {
 "message_diseaseid":"17",
 "message_disease":"2型糖尿病"
 },
 {
 "message_diseaseid":"18",
 "message_disease":"糖尿病肾病"
 }
 ],
 [
 {
 "message_diseaseid":"19",
 "message_disease":"糖尿病性周围精神病"
 },
 {
 "message_diseaseid":"20",
 "message_disease":"自主神经性病变"
 }
 ],
 [
 {
 "message_diseaseid":"21",
 "message_disease":"高血压"
 },
 {
 "message_diseaseid":"22",
 "message_disease":"视网膜病变"
 },
 {
 "message_diseaseid":"23",
 "message_disease":"恶性肿瘤"
 },
 {
 "message_diseaseid":"24",
 "message_disease":"脑卒中"
 },
 {
 "message_diseaseid":"25",
 "message_disease":"重性精神疾病"
 },
 {
 "message_diseaseid":"26",
 "message_disease":"结核病"
 },
 {
 "message_diseaseid":"27",
 "message_disease":"肝炎"
 },
 {
 "message_diseaseid":"28",
 "message_disease":"先天畸形"
 },
 {
 "message_diseaseid":"29",
 "message_disease":"其它"
 }
 ]
 ]
 }
 }

下面是我构建的实体类

这是最里层的两个,

//疾病名称
public class JiBingdataname {
private String message_diseaseid;
private String message_disease;
}
//疾病类型
public class JiBingdata {
private String message_disease_type;
private String message_disease_typeid;
}
//外一层
public class JiBinglist {
 private List mem_message_records;
 private List mem_message_recordss;
}
//最外层
public class JiBingbean {
private String code;
private String message;
private JiBinglist data;
}

下面是我的解析代码

//解析JSON数据
private class Mythread extends Thread {
    @Override
    public void run() {
        System.out.println("....启动线程");

//网络请求路径
String path = HttpInfo.PATH + HttpInfo.GET_JIBINGLIST + HttpInfo.ID;
 String jsonString = HttpUtils.getJsonContent(path);
 Gson gson = new Gson();
 System.out.println("------------------------" + path);
 //解析json数据
jiBingbean = gson.fromJson(jsonString, JiBingbean.class);
 System.out.println("....启动线程" + jiBingbean);
 //解析成功后发送msg给UI主线程
Message msg = new Message();
 msg.what = 101;
 myHandler.sendMessage(msg);
 }
 }

但是出现这样的问题

    java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 520 path $.data.mem_message_recordss[0]
    logcat日志错误指向这一行, jiBingbean = gson.fromJson(jsonString, JiBingbean.class);
请教一下各位大神,是哪里处理的不好

展开
收起
杨冬芳 2016-07-07 12:09:11 4058 0
1 条回答
写回答
取消 提交回答
  • 阿里云论坛版主,伪Linux运维,完美主义者。
     //外一层
    public class JiBinglist {
    private List mem_message_records;
    private List mem_message_recordss;
    }

    指定泛型

     //外一层
    public class JiBinglist {
    private List<JiBingdata> mem_message_records;
    private List<JiBingdataname> mem_message_recordss;
    }

    你解析的主要逻辑都是正确的,如果还有问题就是哪个小细节没有注意到,你再好好检查检查应该OK

    2019-07-17 19:52:40
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载

相关镜像