java技巧

  1. 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.数组转listString[] 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());
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值