簡體   English   中英

C# 將json序列化和反序列化為txt文件

[英]C# serialize and deserialize json to txt file

我正在使用NewtonSoft在我的 wpf 應用程序中處理 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 一樣存儲它,但這不起作用。 我如何讓這個工作?

任何幫助將不勝感激。 :)

我會這樣做:

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;
}

您的問題是您嘗試從您的文件中獲取客戶列表,而您只保存了一個客戶。

如果您想在您的文件中存儲多個客戶,您必須創建一個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;
}

然后,只需保存 customer 的 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);
    }
}

您可能必須根據自己的需要調整此代碼。

我盡力寫出正確的英語,但請隨時糾正我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM