2017-01-02 143 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