解析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 ...
随机推荐
- 6月13 ThinkPHP框架基础
ThinkPHP 一.php框架基础介绍 真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维 ...
- 11204RAC-dbca建库脚本
SET VERIFY OFFconnect "SYS"/"&&sysPassword" as SYSDBAset echo onspool /u ...
- [codechef July Challenge 2017] Pishty and tree
PSHTTR: Pishty 和城堡题目描述Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格的古堡和非常聪明的熊闻名全国.胡斯特的镇城之宝是就是这么一座古堡,历 ...
- 二十二、Spring MVC与Structs2的区别总结
一.框架的入口 1.Structs2采用Filter(StructsPrepartAndExecuteFilter)来进行实现. 2.SpringMVC采用Servlet(DispatcherServ ...
- Python 正则实现计算器
# !/usr/bin/env/ python3 # -*- coding: utf-8 -*- """用户输入计算表达式,显示计算结果""" ...
- 循环中点击单个事件(巧用this,指向当前对象)
<em id='show' value="<?php echo $member['phone']; ?>" class="sui">&l ...
- Ubuntu 下安装Go语言
https://blog.csdn.net/ceciiiilia/article/details/71483221 (一)从官网安装Go语言 1.对于64位Linux: $ wget https:// ...
- Javaconfig形式配置Dubbo多注册中心
多注册中心,一般用不到,但是某些情况下的确能解决不少问题,可以将某些dubbo服务注册到2套dubbo系统中,实现服务在2套系统间的共用. 网上的配置说明很多,但包括dubbo官方说明文档都是以xml ...
- ssd物体检测模型训练和测试总结
参考网址:github:https://github.com/naisy/realtime_object_detection 2018.10.16ssd物体检测总结:切记粗略地看一遍备注就开始训练模型 ...
- bzoj1666
题解: 简单模拟 按照题目意思来就可以了 代码: #include<bits/stdc++.h> using namespace std; int n,ans; int main() { ...