json.Net保存到文件
本文关键字:存到文件 Net json | 更新日期: 2023-09-27 17:51:23
我有一个脚本保存到文件排序,但是我不能编码它保存到条目:如下所示
{
"entry": [
{ "Name": "John" },
{ "Name": "Anna" },
{ "Name": "Peter" }
]
}
我使用json。Net,下面的代码需要添加到条目:name
string json = JsonConvert.SerializeObject(results, Formatting.Indented);
string path = @"C:'inetpub'wwwroot'JSON'json.Net'results.json";
if (!File.Exists(path))
{
File.WriteAllText(path, json);
}
else
{
File.AppendAllText(path, json);
}
我还没有找到任何好的json代码样本,cheers Paul
我就是这么做的!
UserInfo results = new UserInfo
{
Name = Request["name"],
};
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter jsonWriter = new JsonTextWriter(sw);
jsonWriter.Formatting = Formatting.Indented;
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("Name");
jsonWriter.WriteValue(results.Name);
jsonWriter.WriteEndObject();
string json = sw.ToString();
jsonWriter.Close();
sw.Close();
string path = @"C:'inetpub'wwwroot'JSON'json.Net'results.json";
if (!File.Exists(path))
{
File.WriteAllText(path, json);
}
else
{
File.AppendAllText(path, json);
}
}
// create a class object to hold the JSON value
public class UserInfo
{
public string Name { get; set; }
}