解析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 ...
随机推荐
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- Leetcode 980. 不同路径 III
980. 不同路径 III 显示英文描述 我的提交返回竞赛 用户通过次数42 用户尝试次数43 通过次数46 提交次数60 题目难度Hard 在二维网格 grid 上,有 4 种类型的方格: 1 ...
- Linux svn的搭建和使用
搭建: http://www.cnblogs.com/aLittleBitCool/archive/2011/07/09/2101602.html 使用: http://www.cnblogs.com ...
- 微信订阅号,获取用户openid
在微信后台,启用服务器配置. 服务器URL地址,要通过Token的验证. private void Auth() { string token = ConfigurationManager.AppSe ...
- win10装机重装系统
win10装机 1● u启制件 http://www.laomaotao.org.cn/ http://www.laomaotao.org.cn/ 2● 目标盘 3● 安装 ...
- 【Java算法】条件运算符
利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示. 条件运算符的表达式为: 表达式1?表达式2:表达式3 当表达式1为true ...
- day03 基本数据类型
1.什么是数据类型 变量值即我们 存放的数据 ,数据类型及变量值的类型 2.变量值为何要区分类型 因为变量值使用记录现实世界中事物的特征,针对不同的特征就应该用不同类型的值去标识 3.如何应用数据类型 ...
- Could not load driverClass ${driverClassName} 的解决方案
对项目进行ssm整合的过程中,发现报这个错误:Could not load driverClass ${driverClassName} 不明所以,在网上找了半天,各种答案都有,最后终于找 ...
- Django ajax提交 登录
一.url from django.contrib import adminfrom django.urls import pathfrom appo1 import views urlpattern ...
- 理解JavaScript的运行
JavaScript可以运行在head和body标签中! HTML的脚本必须放在<script></script>标签中间! 浏览器会解释并执行位于script标签中的脚本! ...