从我在json.org上可以看到的,所有JSON字符串应该以{(大括号)开头,而[字符(方括号)表示JSON中的数组元素.
我使用该json4j库,我得到了一个开头的输入[,所以我不认为这是有效的JSON.我简要地看了一下JSON模式,但是我真的发现它没有说明JSON文件不能启动[,或者只能从它开始{.
有没有办法只返回mongodb投影中的属性值?例如,我有一个文件,其属性值为数组.我希望查询中的返回对象只是数组,而不是property: [ .. ].例:
文献:
db.test.insert({ name: "Andrew",
attributes: [ { title: "Happy"},
{ title: "Sad" }
]
});
Run Code Online (Sandbox Code Playgroud)
查询:
db.test.find({name: "Andrew"},{attributes:1, "_id":0});
Run Code Online (Sandbox Code Playgroud)
返回:
{ "attributes" : [ { "title" : "Happy" }, { "title" : "Sad" } ] }
Run Code Online (Sandbox Code Playgroud)
我希望它返回数组:
[ { title: "Happy"},
{ title: "Sad" }
]
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?谢谢
目前,我正在研究新产品,并为公共和内部需求制作REST API。我从{json:api}规范开始,对它感到非常满意,直到遇到一些无法找到答案的问题。
根据JSON API规范,每个资源都必须包含id。
每个资源对象必须包含一个id成员和一个type成员。id和type成员的值必须是字符串。
在很多情况下都可以,但不是全部。
我们的大多数端点都是关于“资源”的
如果我要收集“东西”(http://example.com/things)
{
"data": [{
"type": "things",
"id": "1",
"attributes": {
"title": "first"
},
"links": {
"self": "http://example.com/things/1"
}
}, {
"type": "things",
"id": "1",
"attributes": {
"title": "second"
},
"links": {
"self": "http://example.com/things/2"
}
}]
}
Run Code Online (Sandbox Code Playgroud)
如果我只要求一个“物”资源(http://example.com/things/1)
{
"data": {
"type": "things",
"id": "1",
"attributes": {
"title": "first"
},
"links": {
"self": "http://example.com/things/1"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是如何处理与资源无关且没有ID的终结点呢?
例如,在我们的应用程序中,存在一个端点http://example.com/stats,该端点应返回当前登录用户user的统计信息。喜欢 …