解析JSON 注意解析数据为一个对象的情况.--加一下说明
应用场景: 调某接口时, 获取json数据, 需要对数据进行解析 .
第一种: 只判断接口是否调用成功
{
"code":"10102000",
"data":"kkkkkkk"
}
//解析返回结果
JSONObject responseJsonObject = JSONObject.parseObject(response);
int code = responseJsonObject.getIntValue("code");
if (code == 10102000) {
return 1;
}
第二种: 解析data中的数据
try {
HttpResponse httpResponse = client.doAction(textScanRequest); //获取结果
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
JSONArray taskResults = scrResponse.getJSONArray("data"); //获取数组
for (Object taskResult : taskResults) {
if (200 == ((JSONObject) taskResult).getInteger("code")) {
JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
for (Object sceneResult : sceneResults) { //遍历结果数组
String scene = ((JSONObject) sceneResult).getString("scene");
String suggestion = ((JSONObject) sceneResult).getString("suggestion");
String label = ((JSONObject) sceneResult).getString("label");
//根据scene和suggetion做相关处理
//do something
return label;
}
} else {
System.out.println("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
}
}
} else {
System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
}
} else {
System.out.println("response not success. status:" + httpResponse.getStatus());
}
}
第三种: 解析json中的数据
String response = HttpClientUtil.doGet(url, token, appCode);
//输出响应结果
JSONObject responseJsonObject = JSONObject.parseObject(response);
int code = responseJsonObject.getIntValue("code");
if (code == 10102000) {
JSONArray jsonArray = responseJsonObject.getJSONObject("data").getJSONObject("linkmanList").getJSONArray("data"); //数组中的每条数据, 都是一个对象.
for (int i = 0, n = jsonArray.size(); i < n; i++) {
CompanyCarshipLinkman companyCarshipLinkman = jsonArray.getObject(i, CompanyCarshipLinkman.class);
JSONObject jsonObject = jsonArray.getJSONObject(i); //当i = 0时, 获取第一个对象
String linkmanId = jsonObject.getString("linkmanId");
result.saveResult("linkmanId", linkmanId);
linkmanList.add(companyCarshipLinkman);
}
}
解析JSON 注意解析数据为一个对象的情况.--加一下说明的更多相关文章
- 使用google的GSON解析json格式的数据
GSON是谷歌提供的开源库,用来解析Json格式的数据,非常好用.如果要使用GSON的话,则要先下载gson-2.2.4.jar这个文件,如果是在Android项目中使用,则在Android项目的li ...
- C#解析json和xml数据
C#解析json和xml数据 // 用到的包using Newtonsoft.Json; // using Newtonsoft.Json.Linq; const string value = &qu ...
- 如何解析Json返回的数据
Json在Web开发的用处非常广泛,作为数据传递的载体,如何解析Json返回的数据是非常常用的.下面介绍下四种解析Json的方式: Part 1 var list1 = [1,3,4]; alert( ...
- Ajax--PHP+JQuery+Ajax解析json、XML数据、加载页面
一.JQuery+Ajax用get.post方式提交和请求数据 知识要点: $('#userName').blur(function () { var txt = $(this).val(); $.a ...
- Android中解析JSON形式的数据
1.JSON(JavaScript Object Notation) 定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式, ...
- Delphi用QJSON解析JSON格式的数据
本来用superobject来解析JSON已经够用了,可惜这个东东不能在移动端使用,于是找到QJSON来处理. 这是一个国内高手写开源免费的东西,赞一个. 假入数据如下: {"message ...
- 使用 LitJson 解析Json并读取数据
开发中经常要获取各种数据,而现今比较常见的数据便是Json数据格式,网上也有很多解析Json数据的方法,但是 作为小白的我,对于那些个高大上的方法理解不够,这不找了许久发了这些一个 LitJson 库 ...
- Delphi用QJSON解析JSON格式的数据 【转】
本来用superobject来解析JSON已经够用了,可惜这个东东不能在移动端使用,于是找到QJSON来处理. 这是一个国内高手写开源免费的东西,赞一个. 假入数据如下: {"message ...
- Python3基础 json.loads 解析json格式的数据,得到一个字典
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
随机推荐
- python-django rest framework框架
1.API 接口 是什么,干什么用的? API简单的来说就是一个url - http://www.oldboyedu.com/get_user/ - http://www.oldboyedu.com ...
- java的泛型与反射机制
什么是泛型? 泛型,即“参数化类型”.顾名思义,就是将类型由原来的具体的类型参数化,类似于方法中的变量参数,此时类型也定义成参数形式(可以称之为类型形参),然后在使用/调用时传入具体的类型(类型实参) ...
- ASP.NET Core WebAPI 开发-新建WebAPI项目 转
转 http://www.cnblogs.com/linezero/p/5497472.html ASP.NET Core WebAPI 开发-新建WebAPI项目 ASP.NET Core We ...
- CSS3透明背景+渐变样式
CSS3透明背景+渐变样式 转载自博文:<CSS3透明背景+渐变样式> http://blog.csdn.net/netbug_nb/article/details/44343809 效果 ...
- PythonWEB框架之Flask
前言: Django:1个重武器,包含了web开发中常用的功能.组件的框架:(ORM.Session.Form.Admin.分页.中间件.信号.缓存.ContenType....): Tornado: ...
- 通过BeanPostProcessor理解Spring中Bean的生命周期
通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...
- RocketMQ安装教程
1.下载 http://mirror.bit.edu.cn/apache/rocketmq/ 2.安装 .tar.gz cd alibaba-rocketmq/bin chmod u+x * 3.配置 ...
- Qt画笔实现曲线
效果图: void CurvePoint::paintEvent(QPaintEvent *event) { // 曲线上的点 static QList<QPointF> points = ...
- MFC消息-自定义消息
转:http://blog.csdn.net/penpenandtongtong/article/details/18598907 像MFC的窗口发送消息,可以进行自定义的动作行为,因此很多时候非常有 ...
- ubuntu 挂载虚拟机vdi文件
sudo apt-get install nbd-server nbd-client qemu-kvm # rmmod nbd # modprobe nbd max_part=8 # qemu- ...