访问 JSON 对象中的项目以获取请求
Accessing items in a JSON object for fetch requests
我正在使用 json-server 作为模拟后端数据库。该文件如下所示:
{ partners: [{"id": "partner1", "configurations": [list of objects]},
{"id": "partner2", "configurations": [list of objects]}]}
其他两个类别也是如此。
我希望能够通过 PUT 请求更改配置中对象的值,但我不知道 url 使用什么来访问它。如果我使用 url http://localhost:3000/partners/partner1
,我不会得到孤立的对象列表,也无法更新它们。 urls http://localhost:3000/partners/partner1/configurations
和 http://localhost:3000/partners/partner1.configurations
也不起作用。
有谁知道通过 url 获取命令仅访问对象列表的方法吗?
感谢您的帮助
json-server 在 JSON 文件的结构规范化时效果最佳 - 与将其存储在 SQL 数据库中的方式类似。例如,您的数据可能是这样重组的:
{
"partners": [ { "id": "partner1" }, { "id": "partner2" } ],
"configurations": [{
"partnerId": "partner1",
"foo": "bar",
"bar": "foo"
}, {
"partnerId": "partner1",
"foo": "bar2",
"bar": "foo2"
}, {
"partnerId": "partner2",
"foo": "foo",
"bar": "bar"
}]
}
...等等。使用此结构,json-server 将能够像这样解析请求:
http://localhost:1111/partner/partner1/configurations
...由'joining' 文档第一层不同属性的数据块组成。这种方法有点recommended by by the module's author, typicode.
我正在使用 json-server 作为模拟后端数据库。该文件如下所示:
{ partners: [{"id": "partner1", "configurations": [list of objects]},
{"id": "partner2", "configurations": [list of objects]}]}
其他两个类别也是如此。
我希望能够通过 PUT 请求更改配置中对象的值,但我不知道 url 使用什么来访问它。如果我使用 url http://localhost:3000/partners/partner1
,我不会得到孤立的对象列表,也无法更新它们。 urls http://localhost:3000/partners/partner1/configurations
和 http://localhost:3000/partners/partner1.configurations
也不起作用。
有谁知道通过 url 获取命令仅访问对象列表的方法吗?
感谢您的帮助
json-server 在 JSON 文件的结构规范化时效果最佳 - 与将其存储在 SQL 数据库中的方式类似。例如,您的数据可能是这样重组的:
{
"partners": [ { "id": "partner1" }, { "id": "partner2" } ],
"configurations": [{
"partnerId": "partner1",
"foo": "bar",
"bar": "foo"
}, {
"partnerId": "partner1",
"foo": "bar2",
"bar": "foo2"
}, {
"partnerId": "partner2",
"foo": "foo",
"bar": "bar"
}]
}
...等等。使用此结构,json-server 将能够像这样解析请求:
http://localhost:1111/partner/partner1/configurations
...由'joining' 文档第一层不同属性的数据块组成。这种方法有点recommended by by the module's author, typicode.