java json path_【技术累积】【点】【java】【1】JSONPath

闲聊

以后周中每天一篇这种偏短的文章,周末就发长一点的文章,不然自己实在是懒,懒成了习惯了。。。

开始

首先需要明确的是,这里说的是阿里巴巴的fastjson包中的JSONPath,不是jsonPath,两者干的都是一件事儿,但用法什么的还是有较大不同。

可以大概浏览下这两篇文章:

其实说白了这东西就是做解析json串的,但是有一些比较好用的方法。

当然,其更加强大的事情是当做对象查询语言使用(上一篇引用中很详细的说了)

对于我而言,最吸引我的,是直接从json串的String中取值,判断等。

不多说,上代码:

@Test

public void testJsonPath(){

String jsonText = "{\"name\":\"shitHappens\",\"age\":26,\"gender\":\"male\"}";

JSONObject jsonObject = JSONObject.parseObject(jsonText);

Object output = JSONPath.eval(jsonObject,"$.name");

LOGGER.info("eval结果:{}", JSONObject.toJSONString(output));

}

@Test

public void testJsonPath2(){

String jsonText = "{\"names\":[{\"name\":\"shitHappens\",\"age\":26,\"gender\":\"male\"},{\"name\":\"shitHappens\",\"age\":2,\"gender\":\"male\"}]}";

JSONObject jsonObject = JSONObject.parseObject(jsonText);

Object output = JSONPath.eval(jsonObject,"$.names[1].age");

List ages = (List) JSONPath.eval(jsonObject,"$..age");

LOGGER.info("eval结果:{}", JSONObject.toJSONString(output));

}

@Test

public void testJSONPath(){

String jsonText = "{\"names\":[{\"name\":\"shitHappens\",\"age\":26,\"gender\":\"male\"},{\"name\":\"shitHappens\",\"age\":2,\"gender\":\"male\"}]}";

JSONObject jsonObject = JSONObject.parseObject(jsonText);

String path = "$.names[0].name";

// JSONPath.arrayAdd(jsonObject,"");

JSONPath jsonPath = JSONPath.compile(path);

jsonPath.getPath();

boolean contains = JSONPath.contains(jsonObject,path);

boolean containsValue = JSONPath.containsValue(jsonObject,path,"shitHappens");

int size = JSONPath.size(jsonObject,path);

JSONPath.set(jsonObject,path,"dddd");

LOGGER.info("json串:{}",jsonObject);

}

几个点吧:

eval()方法,取值,关键是后面的路径的语法;

contains(),containsValue(),判断值的有无;

set(),设置值;

其实用起来很方便,关键是用的场景了吧,不过我还用的少,大概是那种String的json串,然后不是完全解析的场景吧。

最后还是学学别个的测试代码吧,用断言实现自动化

public void test_entity() throws Exception {

Entity entity = new Entity(123, new Object());

Assert.assertSame(entity.getValue(), JSONPath.eval(entity, "$.value"));

Assert.assertTrue(JSONPath.contains(entity, "$.value"));

Assert.assertTrue(JSONPath.containsValue(entity, "$.id", 123));

Assert.assertTrue(JSONPath.containsValue(entity, "$.value", entity.getValue()));

Assert.assertEquals(2, JSONPath.size(entity, "$"));

Assert.assertEquals(0, JSONPath.size(new Object[], "$"));

}

public static class Entity {

private Integer id;

private String name;

private Object value;

public Entity() {}

public Entity(Integer id, Object value) { this.id = id; this.value = value; }

public Entity(Integer id, String name) { this.id = id; this.name = name; }

public Entity(String name) { this.name = name; }

public Integer getId() { return id; }

public Object getValue() { return value; }

public String getName() { return name; }

public void setId(Integer id) { this.id = id; }

public void setName(String name) { this.name = name; }

public void setValue(Object value) { this.value = value; }

}

结束

东西还是要一搞好就记录下来的,毕竟,博客这些的目的就是记录,巩固当时的记忆,方便日后的快速回顾么,干!

JSONPath.eval()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值