2017-01-02 99 views
3

我正在使用NewtonSoft来处理我的wpf应用程序中的json。我有一个可以保存到txt文件的客户(不涉及数据库)。我做了这样的:C#将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); 
    } 
} 

但我收到这个错误(不能复制错误):

enter image description here已经尝试存储它像一个jArray但是不工作。我如何得到这个工作?

任何帮助将不胜感激。 :)

+2

嗯,你只写了一个客户的文件,但尝试读取客户的数组......不能正常工作。为什么写一个JArray的顾客不工作?这是你需要做的。 –

+1

您是否打算将多个客户保存在同一个文件中?或者为每个客户在同一位置添加多个文件? –

+2

Off topic:性别应该是男性/女性 – Reniuz

回答

2

我会做这样如下:

public class Customer 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
    public string Address { get; set; } 
    public string Gender { get; set; } 
    public int Age { get; set; } 
} 

public void AddCustomer(Customer newCustomer) 
{ 
    var json = File.ReadAllText(pathToTheFile); 
    var customers = JsonConvert.DeserializeObject<List<Customer>>(json); 
    customers.Add(newCustomer); 
    File.WriteAllText(pathToTheFile", JsonConvert.SerializeObject(customers)); 
} 

public Customer GetCustomer(string id) 
{ 
    var json = File.ReadAllText(pathToTheFile); 
    var customers = JsonConvert.DeserializeObject<List<Customer>>(json); 
    var result = new Customer(); 
    foreach (var c in customers) 
    { 
     if (c.Id == id) 
     { 
      result = c; 
      break; 
     } 
    } 
    return result; 
} 
1

您的问题是,您尝试从您的文件中获得客户列表,而您只保存客户。

如果你想多个客户存放在你的文件,你必须创建一个JArray,并添加你的客户到它:

//The customers array 
private JArray customers = new JArray(); 

//Store new customer in array 
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]) 
    ); 

    //Add customer to customers array 
    customers.add(customer); 

    return 1; 
} 

然后,只需保存客户的JArray:

//Save array 
public void Save() 
{ 

    StreamWriter file = File.CreateText(Settings.databasePath + "customer.json"); 

    using (JsonTextWriter writer = new JsonTextWriter(file)) 
    { 
     //Save JArray of customers 
     customers.WriteTo(writer); 
    } 
} 

您可能必须根据自己的需要修改此代码。

我尽我所能写出正确的英文,但可以自由纠正我。

+0

谢谢,试试这个。 – Jenssen