在Java(任何库)中,从类似于以下内容的json(带有嵌套字段,数组和嵌入式文档)开始:
{
"first" : "1",
"second" : {
"third" : "3",
"fourth" : "4"
},
"fifth" : [
{
"index" : 0,
"value": "something"
},
{
"index" : 1,
"value": "else"
}
]
}
然后应用以下jsonpaths(例如,理想情况下是任何一种jsonpath)
second.fourth
fifth[1].[*]
创建一个如下的json文档:
{
"second" : {
"fourth" : "4"
},
"fifth" : [
{
"index" : 1,
"value": "else"
}
]
}
因此,问题是:不仅可以使用jsonpath来获取数据,而且还可以使用所有嵌套字段并创建新的json作为输入的子集吗?
像往常一样欣赏任何例子
注意:对此进行了很多思考之后,我得出的结论是,我需要的是Mongodb中$project的实现,但使用Java且没有数据库。
最佳答案
如果可以使用lodash,则为对象设置和获取功能。
var source = {
"first" : "1",
"second" : {
"third" : "3",
"fourth" : "4"
},
"fifth" : [
{
"index" : 0,
"value": "something"
},
{
"index" : 1,
"value": "else"
}
]
};
var destination = {};
var path = "second.fourth";
_.set(destination, path, _.get(source, path));
path = "fifth[0]"; //Drawback -> if use fifth[1], then destination.fifth will have 0th element empty and 1st element filled.
_.set(destination, path, _.get(source, path));
否则,您将需要为这些set和get运算符编写javascript。
关于java - Java使用jsonpath从另一个Json创建一个新的Json读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56598463/