我在我的wpf应用程序中使用NewtonSoft来处理json。我有一个可以保存到txt文件中的客户端(不涉及数据库)。我是这样做的:
public int store(string[] reservation)
{
JObject customer = new JObject(
new JProperty("id", this.getNewId()),
new JProperty("name", reservation[0]),
new JProperty("address", reservation[1]),
new JProperty("gender", reservation[2]),
new JProperty("age", reservation[3])
);
using (StreamWriter file = File.CreateText(Settings.databasePath + "customer.json"))
using (JsonTextWriter writer = new JsonTextWriter(file))
{
customer.WriteTo(writer);
}
return 1;
}
结果看起来像这样:
{"id":1,"name":"Lars","address":"Bosch 10","gender":"Man","age":"19"}
然后我尝试像这样获取所有客户:
if(File.Exists(Settings.databasePath + "customer.json"))
{
List<Customer> customers;
using (StreamReader r = new StreamReader(Settings.databasePath + "customer.json"))
{
string json = r.ReadToEnd();
customers = JsonConvert.DeserializeObject<List<Customer>>(json);
}
}
但是我收到了这个错误(无法复制错误):
已经尝试将其存储为jArray,但没有起作用。我该如何让它工作?
任何帮助都将不胜感激。:)