Android解析JSON数组,如果里面有两个数组,要怎么处理 -问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

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

2016-07-07 12:09:11 3633 1

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

{
 "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);
请教一下各位大神,是哪里处理的不好
取消 提交回答
全部回答(1)
  • 鬼才神兵
    2019-07-17 19:52:40
     //外一层
    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

    0 0
相关问答

1

回答

如何在Android中解析JSON数组(非Json对象)

2020-02-08 14:14:02 244浏览量 回答数 1

1

回答

json数据怎么解析的

2018-05-10 20:00:09 1146浏览量 回答数 1

1

回答

json数据怎么解析

2018-05-10 19:59:05 1878浏览量 回答数 1

1

回答

json数据解析问题求解答

2016-07-06 17:54:08 1644浏览量 回答数 1

1

回答

利用shell将json数据解析后排序问题

2016-06-17 16:56:40 2411浏览量 回答数 1

1

回答

json的数据,无法正常获取

2016-03-26 08:34:39 1794浏览量 回答数 1

1

回答

map<String,Model>转成json数据后要怎么解析?

2016-03-20 11:19:30 2648浏览量 回答数 1

1

回答

关于利用shell将json数据解析后排序问题

2016-03-05 12:06:49 3511浏览量 回答数 1

0

回答

apply和lambda函数有什么不同吗?

2019-09-04 14:02:17 315浏览量 回答数 0

1

回答

android 无法直接退出程序

2016-06-16 09:41:53 1692浏览量 回答数 1
+关注
杨冬芳
IT从业
1
文章
9167
问答
问答排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载