- JSONPath使用
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
}
}
}
java代码
System.out.println("\n Book数目:" + JSONPath.eval(jsonObject, "$.store.book.size()"));
System.out.println("第一本书title:" + JSONPath.eval(jsonObject, "$.store.book[0].title"));
System.out.println("price大于10元的book:" + JSONPath.eval(jsonObject, "$.store.book[price > 10]"));
System.out.println("price大于10元的title:" + JSONPath.eval(jsonObject, "$.store.book[price > 10][0].title"));
System.out.println("category(类别)为fiction(小说)的book:" + JSONPath.eval(jsonObject, "$.store.book[category = 'fiction']"));
System.out.println("bicycle的所有属性值" + JSONPath.eval(jsonObject, "$.store.bicycle.*"));
System.out.println("bicycle的color和price属性值" + JSONPath.eval(jsonObject, "$.store.bicycle['color','price']"));
2. ValueFilter 过滤输出json
ValueFilter filter = new ValueFilter() {
@Override
public Object process(Object obj, String s, Object v) {
if (v == null)
return 0;
return v;
}
};
result = JSON.parseObject(JSON.toJSONString(result, filter));
3.字符串格式化(一般用于存储redis的key)
定义格式
public static final String REDIS_KEY_CREDIT_INTF = "creditIntf:%s:%s";
生成key
String key = String.format(Constants.REDIS_KEY_CREDIT_INTF, “hello”, "world");
输出creditIntf:hello:world
4.数组转list
String[] args = cacheStrategyParams.split("\\(|,|\\)");
List<String> paramList = Arrays.asList(args);
5.HashMap新迭代方式
Map<String, VarValueAndScope> variables = null; for (Map.Entry<String, VarValueAndScope> entry : variables.entrySet()) { System.out.print(entry.getKey()+"___"+entry.getValue()); }