我有一个API,它返回JSON数据,如下所示:
{
"data":{
"id": "859545",
"name":"Batman",
"custom-fields": {
"--f-09":"custom-1",
"--f-10":"custom-2",
"--f-11":"custom-3"
},
"tags": [],
"created-at": "2021-09-10T15:45:16Z",
"updated-at": "2022-04-23T11:52:49Z"
}
}对于这个JSON,我想将"--f-09“字段更改为”定制-1,自定义-新“,并将"--f-10”更改为“定制-2,自定义-新”,同时保留所有其他字段。
我知道我可以在request.PATCH中使用Nodejs,但是在这种情况下,我需要再次为我想要避免的请求提供所有数据。我只想更新某些字段,同时保留其他字段。
在本例中,我提供了一个简单的示例,它只包含某些字段,但在我的实际代码中,我有许多字段,这是否意味着我需要再次使用所有字段构建response body json,只需更改--f-09和--f-10
以下是守则:
const jsonBody = {
"data": {
"id": "859545",
"name": "Batman",
"custom-fields": {
"--f-09": "custom-1, custom-new",
"--f-10": "custom-2, custom-new",
"--f-11": "custom-2"
},
"tags": [],
"created-at": "2021-09-10T15:45:16Z",
"updated-at": "2022-04-23T11:52:49Z"
}
}
request.patch('https://reqres.in/api/users',jsonBody,function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
console.log(response.statusCode);
}
}
);这是否意味着我需要在这里再次构建完整的JSON主体,就像我在使用修补程序时构建了上面的jsonBody一样,还是有其他方法可以传递--f-09 and --f-10的值?
发布于 2022-04-04 16:53:14
首先检查一下,您想要更新的参数是在对象的一侧。然后你可以过滤,这只值。然后,可以将对象中的各个部分合并。
https://stackoverflow.com/questions/71740655
复制相似问题