[英]How to JSON Serialize Nested Collections in TypeScript
I am trying to serializing list to Json using TypeScirp but I couldn't find way to handle complex nested collection . 我正在尝试使用TypeScirp将列表序列化为Json,但是我找不到处理复杂的嵌套集合的方法。 Please be kind enough to show some light.
请足够友善显示一些光线。
Below is the sample structure I am trying to convert. 下面是我尝试转换的示例结构。
[
{
"method": "PATCH",
"uri": "/ScrumStarain/_apis/wit/workItems/$Product Backlog Item?api-version=1.0",
"headers": {
"Content-Type": "application/json-patch+json"
},
"body": [
{
"op": "add",
"path": "/fields/System.Title",
"value": "apip1"
},
{
"op": "add",
"path": "/id",
"value": "-1"
}
]
},
{
"method": "PATCH",
"uri": "/ScrumStarain/_apis/wit/workItems/$Task?api-version=1.0",
"headers": {
"Content-Type": "application/json-patch+json"
},
"body": [
{
"op": "add",
"path": "/fields/System.Title",
"value": "apip2"
},
{
"op": "add",
"path": "/id",
"value": "-2"
}
]
}
]
below is the sample what I was trying to do , I am trying to generate it dynamically . 以下是我尝试做的示例,我试图动态生成它。 I was failed to add body and subordinate tags.
我未能添加正文和下级标签。
var state =" xy" ;
var tagsCollection =["a","b"];
var tempBody : any =[];
tempBody.op = "add";
tempBody.path = "/fields/System.State";
tempBody.value = state;
var jsonMainString:any = {};
jsonMainString.method = "PATCH";
jsonMainString.uri="/_apis/wit/workItems/123?api-version=1.0";
jsonMainString.headers = { "Content-Type" :"application/json-patch+json"};
jsonMainString.body = tempBody;
console.log(JSON.stringify(jsonMainString));
and ultimate output looks like below , which is not correct 并且最终输出如下所示,这是不正确的
{"method":"PATCH","uri":"/_apis/wit/workItems/123?api-version=1.0","headers":{"Content-Type":"application/json-patch+json"},"body":[]}
try JSON.stringify(object)
would be hleped you & change the code to var tempBody : any ={};
尝试
JSON.stringify(object)
会给您带来麻烦,并将代码更改为var tempBody : any ={};
var data= [ { "method": "PATCH", "uri": "/ScrumStarain/_apis/wit/workItems/$Product Backlog Item?api-version=1.0", "headers": { "Content-Type": "application/json-patch+json" }, "body": [ { "op": "add", "path": "/fields/System.Title", "value": "apip1" }, { "op": "add", "path": "/id", "value": "-1" } ] }, { "method": "PATCH", "uri": "/ScrumStarain/_apis/wit/workItems/$Task?api-version=1.0", "headers": { "Content-Type": "application/json-patch+json" }, "body": [ { "op": "add", "path": "/fields/System.Title", "value": "apip2" }, { "op": "add", "path": "/id", "value": "-2" } ] } ]; console.log(JSON.stringify(data));
You maybe should look up on how to manipulate Arrays in JavaScript . 您可能应该研究如何在JavaScript中操作数组 。 So I guess what you are trying is getting the body populated.
因此,我想您正在尝试的方法是填充人体。
var state =" xy" ;
var tagsCollection =["a","b"];
var tempBody =[];
tempBody.push({
op: "add",
path: "/fields/System.State",
value: state
});
var jsonMainString = {};
jsonMainString.method = "PATCH";
jsonMainString.uri="/_apis/wit/workItems/123?api-version=1.0";
jsonMainString.headers = { "Content-Type" :"application/json-patch+json"};
jsonMainString.body = tempBody;
console.log(JSON.stringify(jsonMainString));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.