解析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 ...
随机推荐
- Matlab:非线性热传导(抛物方程)问题
函数文件1:real_fun.m function f=real_fun(x0,t0) %精确解 f=4*x0*(1-x0)*sin(t0); 函数文件2:F.m function f=F(N,u,U ...
- 【实战问题】【2】Ambiguous mapping found. Cannot map 'xxController.Create' bean method
正文: 启动项目时出现该报错. 原因为:在controller中url映射出现重复,@RequestMapping(value = "user/create"). 解决方案为:全局 ...
- 把javabean复制到另一个javabean 使用BeanUtils.copyProperties(a,b) 复制
该方法对于两种不同的jar包有两种不同的意义 ,a,b通常是两个结构相似的javabean,注意:a,b里的定义类型名称必须一致才能复制 引用的是org.springframework.beans 则 ...
- 浅谈MVVM
MVVM 模式将 Presenter 改名为 ViewModel,基本上与 MVP 模式完全一致. 唯一的区别是,它采用双向绑定(data-binding):View的变动,自动反映在 ViewMod ...
- ActiveMQ broker和客户端之间的确认
生产者发送消息:producer ---------> broker broker返回确认:broker ---------> producer 生产者发送同步消息,broker会返回Re ...
- rsyslog的配置文件使用方法
参考地址: http://www.rsyslog.com/doc/v8-stable/configuration/property_replacer.html rsyslog消息模板的定义规则 &qu ...
- MINIUI应用
MINIUI是一款优秀的JS前端web框架,提供丰富.强大控件库,能快速开发企业级Web应用软件. 属于付费插件. 如果有兴趣推荐去这个网址看看.MiniUI 在线示例 http://www.min ...
- js--阻止冒泡,捕获,默认行为
防止冒泡和捕获 w3c的方法是e.stopPropagation(),IE则是使用e.cancelBubble = true· var el = window.document.getElementB ...
- Author and Submission Instructions
This document contains information about the process of submitting a paper to NIPS 2014. You can als ...
- html盒子水平和垂直居中
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...