json - 从框架的 JSON-LD 中删除额外的参数
问题描述
因此,让我们考虑从 db 获取的以下数据:
[
{
"@id": "http://example.com/1",
"http://example.com/label": "Parent",
"http://example.com/status": "Active",
"http://example.com/children": [
{
"@id": "http://example.com/2"
}
]
},
{
"@id": "http://example.com/2",
"http://example.com/label": "Child",
"http://example.com/status": "Active"
}
]
和框架:
{
"@context": {
"@base": "http://example.com/",
"@vocab": "http://example.com/"
},
"@graph": {
"status":{}
}
}
结果将如下所示:
{
"@context": {
"@base": "http://example.com/",
"@vocab": "http://example.com/"
},
"@graph": [
{
"@id": "1",
"children": {
"@id": "2",
"label": "Child",
"status": "Active"
},
"label": "Parent",
"status": "Active"
},
{
"@id": "2",
"label": "Child",
"status": "Active"
}
]
}
正如您在第一个对象中看到的那样,在该children
部分中,除了 id 之外,我还获得了一些额外的参数。
有没有办法可以简化children
列表以仅包含 id:
"children": [
"2"
]
我尝试将其添加到我的框架中:
"children": {
"@id": "http://example.com/children",
"@type": "@id"
}
但它不像我预期的那样工作。
解决方案
使用框架标志:"@embed": "@never"
或"@explicit": true
.
{
"@context": {
"@base": "http://example.com/",
"@vocab": "http://example.com/"
},
"@graph": {
"status": {},
"@embed": "@never"
}
}
或者
{
"@context": {
"@base": "http://example.com/",
"@vocab": "http://example.com/"
},
"@graph": {
"status": {},
"children": {"@explicit": true, "@omitDefault": true}
}
}
但也许你所需要的只是压缩。
如果您不想压缩数组,请切换相应的选项。在 JSONLD-Java 中:
final JsonLdOptions options = new JsonLdOptions();
options.setCompactArrays(false);
推荐阅读
- .net - MongoDB 获取文档字段描述(如 SQL 中的系统表)
- opencv - cv::dnn::Layer::forward 不适用于特定层(python)
- javascript - 当有多个绘图出错时悬停消息的位置
- twilio - Twilio Chat - 除非频道成员,否则无法获取公共频道“services.publicChannels 未定义”
- mysql - mysqli 多案例排序查询
- java - Gradle lint 和测试构建错误 - 库中的 JAXBException 和无效的包引用
- powershell - 当作为参数给出时,仅从字符串/数组中获取第一个值
- sql - 有条件地将值从一行复制到另一行
- react-native - 每当我尝试在移动设备上构建我的 react-native 应用程序时,都会出现“应用程序已停止”消息
- jquery - 如何在按钮单击时获得最后一个复选框选中的值