JsonUtil工具类
com.justep.util.JsonUtil
- 我们封装了一些JsonPath的工具函数
- 主要是根据path返回期望类型的值
getString(JSONObject json, String path)
功能:根据path获取对应的值,结果强制转换成String
参数:
json:需要求值的JsonObject
path:需要求值的path
返回:
类型:String
参考:
json:{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"isbn": "0-553-21311-3"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
getString(json, "$.store.book[0].author")//返回值为Nigel Rees
getInteger(JSONObject json, String path)
功能:根据path获取对应的值,结果强制转换成Integer
参数:
json:需要求值的JsonObject
path:需要求值的path
返回:
类型:Integer
getJSONObject(JSONObject json,String path)
功能:根据path获取对应的值,结果强制转换成JsonObject
参数:
json:需要求值的JsonObject
path:需要求值的path
返回:
类型:JsonObject
toJSONString(JSONObject json)
功能:将JSONObject转换为JsonString
参数:
json:需要转换的JsonObject
返回:
类型:JsonString