是否有可能从当前的对象中获取一个新的JsonPath对象?我发现的唯一一件事是setRootPath(),但这并不是我所要寻找的,因为一旦设置了根,就不能“返回”。
我的用例:
我有一个JSON对象数组。我想遍历JSON数组,并将每个对象传递给某个函数。问题是,我不想反序列化对象,也不想传递LinkedHashMap。相反,我想传递一个单独表示对象的JsonPath。
附加信息:
让我们假设我们有以下JSON:
[
{"name": "David"},{"name": "Dana"},{"name": "Ron"}
]我们刚刚用JsonPath阅读了它:
JsonPath = JsonPath.from(json);我想对这些对象进行迭代,对于每个对象都有一个JsonPath来表示它。做这件事的唯一方法(据我所知)是使用setRootPath(),但是一旦你这样做了,你就不能回去了。
发布于 2022-09-22 18:44:05
//A bit confusing about what was asked here. But try this, hope it will help
//If you have a json object already no need to initiate the jsonObject
JSONObject jsonObject = new JSONObject();
String jsonString = jsonObject.toString();
String path = "$.rootObject.childObject"
//Only returning the child object
JSONObject j = JsonPath.read(jsonString, path);
//Returning the array of string type from the child object. E.g
//{"root": "child":[x, y, z]}
List<String> values = sonPath.read(jsonString, path);https://stackoverflow.com/questions/63900531
复制相似问题