我正在尝试编写简单的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 中添加一个新对象,但我想添加一个字段
如何完成此任务?
P.S 我正在使用Newtonsoft.JSON库。
Edit 切换到 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 = "[{rn "YEAR": "2018-2019",rn "COMPNO": "1",rn "TYPE": "SAL",rn "Field1": false rn },rn {rn "YEAR": "2018-2019",rn "COMPNO": "2",rn "TYPE": "PUR",rn "Field1": falsern }]";
var array = JArray.Parse(initialJson);
array.ToList().ForEach(item => item["Field2"] = false);
var result = array;