我正在尝试编写简单的Json Array。
我对这个Json有点生锈,因为我刚开始通过 Vb.Net 和使用 NewtonSoft.Json 库
很抱歉,如果有任何问题。正如我所说的,我刚开始Json,所以请提出纠正意见。
老杰森:
[{
"YEAR": "2018-2019",
"COMPNO": "1",
"TYPE": "SAL",
"Field1": false
},
{
"YEAR": "2018-2019",
"COMPNO": "2",
"TYPE": "PUR",
"Field1": false
}]
我希望在对象中添加一个字段
新杰森:
[{
"YEAR": "2018-2019",
"COMPNO": "1",
"TYPE": "SAL",
"Field1": false,
"Field2": false '-----------------------Something Like this
},
{
"YEAR": "2018-2019",
"COMPNO": "2",
"TYPE": "PUR",
"Field1": false,
"Field2": false '--------------In every object if possible.
}]
我的Json很简单,没有团队。
我知道如何在Jarray中添加新对象,但我想添加一个字段
我该如何完成这项任务?
PS我正在使用 Newtonsoft.JSON 库。
答案 0 :(得分:2)
编辑已切换到VB
Dim initialJson = "[{" & vbCrLf & " ""YEAR"": ""2018-2019""," & vbCrLf & " ""COMPNO"": ""1""," & vbCrLf & " ""TYPE"": ""SAL""," & vbCrLf & " ""Field1"": false " & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""YEAR"": ""2018-2019""," & vbCrLf & " ""COMPNO"": ""2""," & vbCrLf & " ""TYPE"": ""PUR""," & vbCrLf & " ""Field1"": false" & vbCrLf & " }]"
Dim array = JArray.Parse(initialJson)
For Each item In array
item("Field2") = False
Next
Dim Result = array
如果要创建该json,我建议您在创建它之前先做,但是如果您只需要更新该数组。
var initialJson = "[{\r\n \"YEAR\": \"2018-2019\",\r\n \"COMPNO\": \"1\",\r\n \"TYPE\": \"SAL\",\r\n \"Field1\": false \r\n },\r\n {\r\n \"YEAR\": \"2018-2019\",\r\n \"COMPNO\": \"2\",\r\n \"TYPE\": \"PUR\",\r\n \"Field1\": false\r\n }]";
var array = JArray.Parse(initialJson);
array.ToList().ForEach(item => item["Field2"] = false);
var result = array;