JsonPath是从JSON文档中抽取指定信息的工具。提供多种语言实现版本,包括JavaScript,java等。那jsonpath怎么找数据?今天我们来给大家分享一下关于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
依赖包
com.jayway.jsonpath json-path 2.4.0 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[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"; } } }
用java的JsonPath方法提取不仅快速而且方便好用,我们可以根据JSON路径去取,去提取有用的价值内容。最后大家如果想要了解更多json相关知识,敬请关注奇Q工具网。
推荐阅读: