解析三层json数组步骤
来源:互联网 发布:思科交换机端口做acl 编辑:程序博客网 时间:2024/05/17 01:41
最近开发中需要解析json数组,因为是接手的是同事的代码,发现代码中用了解析json的四个jar包,所以普通的json数组解析不能实现想要的效果,再此写下步骤。
首先发现了这两行代码:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
所以发现使用了jar包。
然后从同事那里获得的json数组是这样的:
u_depresult = {"IsError":false,"Message":"success","Data":[{"dep_id":"2016051221302058642057108414","dep_pid":"root","dep_name":"办公室","dep_desc":null,"dep_code":null,"dep_order":null,"dep_history":null,"create_user":null,"create_date":"2016-05-12 21:30:20","update_date":null,"update_user":null,"is_import":"T"}]}
我们需要用到“dep_name”的数据。
观察json数组发现Data是一个数组【】。
所以解析步骤如下:
try {
if (u_depresult == null) {
msg.obj = "连接服务器超时,请稍后再试!";
} else {
JSONObject recidObj = JSONObject.fromObject(u_depresult);
if (recidObj.get("IsError").toString().equals("true")) {
msg.obj = recidObj.get("Message");
} else {
SONArray recidObj01 = JSONArray.fromObject(recidObj.get("Data"));
u_dep = recidObj01.getJSONObject(0).getString("dep_name");
}
}
} catch (Exception e) {
// TODO: handle exception
}
if (u_depresult == null) {
msg.obj = "连接服务器超时,请稍后再试!";
} else {
JSONObject recidObj = JSONObject.fromObject(u_depresult);
if (recidObj.get("IsError").toString().equals("true")) {
msg.obj = recidObj.get("Message");
} else {
SONArray recidObj01 = JSONArray.fromObject(recidObj.get("Data"));
u_dep = recidObj01.getJSONObject(0).getString("dep_name");
}
}
} catch (Exception e) {
// TODO: handle exception
}
1.解析是要注意是使用的JSONbject.fromObject(),这是jar包的一个方法
2.因为Data是一个数组,所以要用JSONArray。
3.recidObj.get("Data")是取出在recidObj 中的Data数组。
4.Data数组只有一层,因此getJSONObject(0)就可以了,然后getString取出dep_name。
5.完成。
0 0
- 解析三层json数组步骤
- Gson解析Json步骤
- JSON解析步骤
- java解析JSON步骤
- 解析JSON数据的步骤
- JSONObject 解析json的步骤
- GSON之JSON解析步骤
- 超详细JSON解析步骤
- 超详细JSON解析步骤
- 超详细JSON解析步骤
- java 中解析json步骤
- 超详细JSON解析步骤
- 超详细JSON解析步骤
- Flex解析Json数组
- 解析json数组
- json数组解析
- JSON数组解析
- JSON解析数组
- Linux 内核和驱动开发工程师的发展前景怎么样
- mapreduce的二次排序(字符型)
- Centos7搭建SVN服务器
- 判断是否是微信浏览器打开
- android基础
- 解析三层json数组步骤
- phpstorm 调试总是断点在index.php的第一行
- 你不可不知的HTML优化技巧
- windows server 添加多个IP
- 安装VMware虚拟机出现the msi ''c:\user_x64.msi failed报错信息解决方案
- QT error LNK2019 的问题
- Activity、Window、View三者的差别,fragment的特点?
- TCP粘包
- 程序设计1