JsonPath如何获取JSON数据中的值
场景: 发送接口请求后,得到请求结果值是Json数据, 需要从Json数据信息中提取字段值。
响应值字符与字符之间有空格,导致用正则表达式方法提取比较麻烦,于是用java的JsonPath方法提取快速方便好用,根据JSON路径去取。
Json数据: 需要提取FILE 字段对应的值。
{
"data" : {
"testCaseData" : [
{
"agent_version" : "9.7.0.2225",
"android_id" : "e3d699cf01620531",
"asset_number" : "",
"FILE" : "./ wwwccko(33) .zip",
"noncomp_reason" : "",
},
{
"agent_version" : "2.0.0.1518",
"android_id" : "",
"asset_number" : "",
"FILE" : "./XXXX(22) .zip",
"noncomp_reason" : "",
}
],
"total_count" : 2
},
"error_code" : 1,
"message" : "Success",
"timestamp" : 1504765848
}
解决方法:
package com.app.test; import java.util.LinkedHashMap;
import net.minidev.json.JSONArray;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException; public class TestJsonPath { public static void main(String[] args) {
String charset = "utf-8";
File file = new File("D:\\Work_Report\\JSON2.txt");
long fileByteLength = file.length();
byte[] content = new byte[(int) fileByteLength];
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
fileInputStream.read(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String str = null;
try {
str = new String(content, charset);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(str);
readjson(str, "$.data.testCaseData[0].FILE");
System.out.println(readjson(str, "$.data.testCaseData[0].FILE"));
} public static String readjson(String json, String jsonPath) {
try {
Object value = JsonPath.read(json, jsonPath, new Predicate[0]);
if (value instanceof Integer) {
return value.toString();
} else if (value instanceof String) {
return value.toString();
} else if (value instanceof Boolean) {
return value.toString();
} else if (value instanceof JSONArray) {
JSONArray arr = (JSONArray) value;
if (!arr.isEmpty()) {
return arr.toJSONString();
} else
return "";
} else if (value instanceof LinkedHashMap) {
return value.toString();
} else if (value instanceof Float) {
return value.toString();
} else {
return value.toString();
}
} catch (Exception e) {
return "pathnotfound";
}
}
}
注: JsonPath固定语法: $.XX.XX[索引].对象key
依赖包
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
String rep.jsonpath.result;
rep=repv.getResponseDataAsString();
jsonpath = "$.data.device[1].version
result = readjson(pre,jsonpath)
vars.put("XX",result)
工具类:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import net.minidev.json.JSONArray;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate; public class Utils { public static String readJson(String filePath) {
String charset = "utf-8";
File file = new File(filePath);
long fileByteLength = file.length();
byte[] content = new byte[(int) fileByteLength];
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
fileInputStream.read(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String str = null;
try {
str = new String(content, charset);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str;
} public static String readjson(String json, String jsonPath) {
try {
Object value = JsonPath.read(json, jsonPath, new Predicate[]);
if (value instanceof Integer) {
return value.toString();
} else if (value instanceof String) {
return value.toString();
} else if (value instanceof Boolean) {
return value.toString();
} else if (value instanceof JSONArray) {
JSONArray arr = (JSONArray) value;
if (!arr.isEmpty()) {
return arr.toJSONString();
} else
return "";
} else if (value instanceof LinkedHashMap) {
return value.toString();
} else if (value instanceof Float) {
return value.toString();
} else {
return value.toString();
}
} catch (Exception e) {
return "pathnotfound";
}
} }
JsonPath如何获取JSON数据中的值的更多相关文章
- Java如何获取JSON数据中的值
场景:在接口自动化场景中,下个接口发送的请求参数,依赖上个接口请求结果中的值.需要将获取值作为全局参数引用. import java.io.File; import java.io.FileInput ...
- js 获取json串中的值
用js中著名的eval函数var strJSON = "{name:'json name'}";//得到的JSONvar obj = eval( "(" + s ...
- java获取json格式中的值
先右键项目,然后点击properties,然后选中java Builder Path,选择add external jars,引入需要引入json.jar package web; import or ...
- C#调用接口返回json数据中含有双引号 或其他非法字符的解决办法
这几天,调用别人接口返回json数据含有特殊符号(双引号),当转换成json对象总是报错, json字符格式如下 { "BOXINFO":[ { ", "ITE ...
- ajax获取json数据为undefined--原因解析
解决办法:var dataObj=eval("("+data+")");//转换为json对象 问题: 1. 碰到一个问题ajax成功获取json数据后,取值显 ...
- 获取JSON对象的属性值
1.问题背景 有一个json对象,其中有键值对,那怎样获取json对象中属性值 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- mysql json_extract函数获取json字段中某个key的值
参考:https://www.cnblogs.com/chuanzhang053/p/9139624.html json_extract函数可以获取json对象中指定key的值,用法:json_ext ...
- Netflix Falcor获取JSON数据
Netflix开源了JavaScript库Falcor,它为从多个来源获取JSON数据提供了模型和异步机制. Netflix利用Falcor库实现通过JSON数据填充他们网页应用的用户界面.所有来自内 ...
- jquery通过ajax方法获取json数据不执行success
1.jquery通过ajax方法获取json数据不执行success回调 问题描述:jquery通过ajax方法获取json数据不执行success回调方法 问题原因:json格式存在问题或不符合标准 ...
随机推荐
- Charles 抓包的简单使用
1.准备工具: 软件 Charles 手机 随意哪个现代手机 2.基本配置 安装Charles的电脑和手机在同一个局域网下, 点击手机上的和电脑练得同一个局域网的名字进行配置,里面有个代理,选择手动, ...
- 51nod--1174 区间中最大的数 (RMQ)
题目: 1174 区间中最大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j ...
- Caffe+Matlab'hole
有时候,多坚持一小下下就成功了,遇到问题就频繁重装系统并不可取!放弃很容易,但坚持真的很酷! 1.安装依赖库也能出问题 命令行输入: sudo apt-get install libprotobuf- ...
- Intsall The Nessus in you kali linux
1.first you shold download the nessus on the web station the nessus download site url: https://www. ...
- 为什么用Flow
Flow 是 facebook 出品的 JavaScript 静态类型检查工具.Vue.js 的源码利用了 Flow 做了静态类型检查,所以了解 Flow 有助于我们阅读源码. flow的工作方式? ...
- Angular项目中核心模块core Module只加载一次的实现
核心模块CoreModule在整个系统中只加载一次,如何实现? 创建core Modele:ng g m core 既然CoreModule是类,就有构造函数,在构造函数中进行依赖注入. export ...
- Leetcode中值得一做的题
3.longest substring Given a string, find the length of the longest substring without repeating chara ...
- In the beginning, Coders create the repos and blogs
---恢复内容开始--- 这是一个新的博客 print(‘hello new world’) 这个博客叫做 brain in a jar, 不知道以后会不会改名. 只是因为偶然想到了普特南的缸中之脑理 ...
- Python字符串的相关操作
1.大小写转换 判断字符串 s.isalnum() #所有字符都是数字或者字母 s.isalpha() #所有字符都是字母 s.isdigit() #所有字符都是数字 s.islower() #所有字 ...
- bootstrap_响应式布局简介_媒体查询_媒体选择器_2x3x图
响应式布局 在不同设备上,同一网页根据设备特性(显示屏大小,分辨率)呈现不同的布局样式. 思考: 获取设备相关信息 将屏幕划分为几个区域 给需要变化的结构写多套 css 样式 媒体查询 常用写法 @m ...