我正在使用 json.NET 在 asp.net 中使用 json,其中来自文本框的按钮单击值被添加到名为 country.json 的 json 文件中。有两个以国家及其首都为值的文本框,country.json 文件如下所示,
[
{
"country":"USA",
"capital":"New York"
},
{
"country":"China",
"capital":"Bejing"
},
{
"country":"India",
"capital":"New Delhi"
}
]
我能够使用一个节点创建 json,但是如何将第二个节点附加或添加到现有 json。这是c#代码,
public class country
{
public string Country { get; set; }
public string Capital { get; set; }
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
country ctry = new country();
ctry.Country = txtCtry.Text;
ctry.Capital = txtCapital.Text;
File.AppendAllText(MapPath("Data/countrycaps.json"),JsonConvert.SerializeObject(ctry,Formatting.Indented));
}