[英]Java create a new Json reading from another Json using jsonpath
在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,那么对象就有set和get函数。
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.