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

这篇博客介绍了阿里巴巴的Fastjson包中的JSONPath用法,它用于解析和操作JSON字符串。文中通过示例代码展示了如何使用eval()方法取值、contains()和containsValue()判断值的存在以及set()方法设置值。还提供了测试用例来演示自动化验证。Fastjson的便捷之处在于能直接从JSON字符串中获取和修改值,适用于不完全解析的场景。
摘要由CSDN通过智能技术生成

闲聊

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

开始

首先需要明确的是,这里说的是阿里巴巴的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
    评论
要将JSON文档数组插入MongoDB,可以使用MongoDB的Java驱动程序和BSON库来实现。 下面是一个简单的示例代码,演示如何将JSON文档数组插入MongoDB。 ```java import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import org.json.JSONArray; import org.json.JSONObject; public class InsertJsonArrayToMongoDB { public static void main(String[] args) { // 创建MongoDB客户端 MongoClient mongoClient = new MongoClient("localhost", 27017); // 获取要使用的数据库 MongoDatabase db = mongoClient.getDatabase("test"); // 获取要使用的集合 MongoCollection<Document> collection = db.getCollection("mycollection"); // 创建JSON文档数组 JSONArray jsonArray = new JSONArray(); JSONObject json1 = new JSONObject(); json1.put("name", "John"); json1.put("age", 30); jsonArray.put(json1); JSONObject json2 = new JSONObject(); json2.put("name", "Mary"); json2.put("age", 25); jsonArray.put(json2); // 将JSON文档数组转换为BSON文档数组 Document[] docs = new Document[jsonArray.length()]; for (int i = 0; i < jsonArray.length(); i++) { JSONObject json = jsonArray.getJSONObject(i); Document doc = Document.parse(json.toString()); docs[i] = doc; } // 将BSON文档数组插入MongoDB collection.insertMany(Arrays.asList(docs)); // 关闭MongoDB客户端 mongoClient.close(); } } ``` 需要注意的是,上述代码中将JSON文档数组转换为BSON文档数组时,使用了`Document.parse(json.toString())`方法。这是因为MongoDB的Java驱动程序不支持直接将JSONObject对象转换为BSON文档对象,需要先将JSONObject对象转换为JSON字符串,再使用`Document.parse()`方法将JSON字符串转换为BSON文档对象。 如果在插入JSON文档数组时出现错误,可以检查JSON文档的格式是否正确,以及转换为BSON文档时是否有误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值