更改 JSON 结构以在 API post 请求中发送
Change JSON structure to send in API post request
我正在 API POST 调用 Angular 8. 我必须在调用中发送一个 JSON 对象,该对象应该是结构:
-{}JSON
-{}data
-[]exp
+{} 0
+{} 1
但我正在以这种格式发送数据:
-[]JSON
+{} 0
+{} 1
在我的打字稿中,我在名为 receivedData
的数组中得到两个对象 {}0, {}1
然后我将数据存储为:
this.changedData = this.receivedData;
this.postService.postMethod(this.headers, this.changedData)
在我的 postService 中:
postMethod(header, changedData): Observable<any[]> {
return this.http.post<any>(`the url here`, changedData, {headers: header, responseType: 'text' as 'json'})
.pipe(map(response => {
return response;
}))
}
如何以上述格式发送数据?我希望 changedDetails
的 json 结构与顶部提到的一样,具有相同的命名约定,例如: {}data and []exp
如何将 receivedData objects
推入 exp[]
其中然后我可以发送到 data{}
然后将完全推入 changedDetails {}
为了让我们达成共识,我假设您正在接收具有以下形状的数据:
[ { ... }, { ... } ]
你想把它变成这个形状:
{
data: {
exp: [ { ... }, { ... } ]
}
}
(如果不是这种情况请告诉我。)
如果这是正确的,那么转换就非常简单:只需像这样创建一个新的对象文字:
this.changedData = {
data: {
exp: this.receivedData,
},
};
我正在 API POST 调用 Angular 8. 我必须在调用中发送一个 JSON 对象,该对象应该是结构:
-{}JSON
-{}data
-[]exp
+{} 0
+{} 1
但我正在以这种格式发送数据:
-[]JSON
+{} 0
+{} 1
在我的打字稿中,我在名为 receivedData
的数组中得到两个对象 {}0, {}1
然后我将数据存储为:
this.changedData = this.receivedData;
this.postService.postMethod(this.headers, this.changedData)
在我的 postService 中:
postMethod(header, changedData): Observable<any[]> {
return this.http.post<any>(`the url here`, changedData, {headers: header, responseType: 'text' as 'json'})
.pipe(map(response => {
return response;
}))
}
如何以上述格式发送数据?我希望 changedDetails
的 json 结构与顶部提到的一样,具有相同的命名约定,例如: {}data and []exp
如何将 receivedData objects
推入 exp[]
其中然后我可以发送到 data{}
然后将完全推入 changedDetails {}
为了让我们达成共识,我假设您正在接收具有以下形状的数据:
[ { ... }, { ... } ]
你想把它变成这个形状:
{
data: {
exp: [ { ... }, { ... } ]
}
}
(如果不是这种情况请告诉我。) 如果这是正确的,那么转换就非常简单:只需像这样创建一个新的对象文字:
this.changedData = {
data: {
exp: this.receivedData,
},
};