当我编写这段代码时:
string[] title= {"title1","title2","title3","title4"};
int[] iid= {124,122,333,234};
dynamic bucket = new JObject();
bucket.bucket = "Parking Lot";
bucket.roadmap = new JArray();
dynamic issue = new JObject();
dynamic json2 = new JArray(bucket);
for(int i=0; i<title.Length; i++) {
issue.title = title[i];
issue.iid = iid[i];
issue.order = i;
bucket.roadmap.Add(issue);
}
Console.WriteLine(json2.ToString());
我的结果是:
[
{
"bucket": "Parking Lot",
"roadmap": [
{
"title": "title4",
"iid": 234,
"order": 3
},
{
"title": "title2",
"iid": 122,
"order": 1
},
{
"title": "title3",
"iid": 333,
"order": 2
},
{
"title": "title4",
"iid": 234,
"order": 3
}
]
}
]
重复了第一个和最后一个“路线图” JObjects。有没有一种方法可以动态生成带有我要传递的唯一值的JSON?
我也尝试非动态地做。
JArray bucket = new JObject();
bucket.Add("bucket","Parking Lot");
bucket.Add("order", new JArray());
JObject issue = new JObject();
...
但是我找不到选择“ bucket.order.Add”向该JArray添加问题的方法。
最后,我只是尝试遍历带有大量数据的JArray,我想将其与存储桶类型分离,提取每个issue值并将其存储在新的JSON中,结构如下所示。
谢谢
最佳答案
在添加到阵列之前,请创建一个新的问题对象。