读取、写入和追加JSON文件

本文关键字:追加 JSON 文件 读取 | 更新日期: 2023-09-27 18:18:25

我一直试图正确解析一些json文件一段时间了,但我似乎不能得到它的权利。我尝试了许多教程和json.net库,但似乎找不到正确的方法来实现这一点。我的文件都有相同的格式:

file.json:

{
  "Expense": {
    "Name": "something",
    "Amount": "34.90",
    "Due": "28/12/2011",
    "Recurrence": "1 Months",
    "Paid": "0",
    "LastPaid": "01/01/2002"
  }
}
{
  "Expense": {
    "Name": "OneTel Mobile Bill",
    "Amount": "39.90",
    "Due": "28/12/2011",
    "Recurrence": "1 Months",
    "Paid": "0",
    "LastPaid": "01/01/2002"
  }
}
{
  "Expense": {
    "Name": "some other Bill",
    "Amount": "44.90",
    "Due": "28/12/2011",
    "Recurrence": "1 Months",
    "Paid": "0",
    "LastPaid": "01/01/2002"
  }
}

现在,我有两个问题。但是首先,我知道如何编写json文件。所以这对我来说不是问题。但我的两个问题是:

如何读取json字符串/文件:(psuedocode)

FOREACH VAR EXPENSE IN EXPENSES 
OUTPUT EXPENSE.NAME 
OUTPUT EXPENSE.AMOUNT 

等等……

第二个问题是:

如何添加一个全新的"费用"到现有的json文件?

我将非常感谢你的帮助。

谢谢

我正在将json写入文件,如下所示:

    //Create the Json file and save it with WriteToFile();
    JObject jobject =
        new JObject(
            new JProperty("Expense",
                new JObject(
                    new JProperty("Name", NameTextBox.Text),
                    new JProperty("Amount", AmountTextBox.Text),
                    new JProperty("Due", DueTextBox.Text),
                    new JProperty("Recurrence", EveryTextBox.Text + " " + EveryComboBox.SelectionBoxItem),
                    new JProperty("Paid", "0"),
                    new JProperty("LastPaid", "Never")
                            )
                        )
                   );            
    try
    {
        WriteToFile(Expenses, jobject.ToString());
        // Close the flyout now.
        this.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
    }
    catch (Exception exception)
    {
        Debug.Write(exception.Message);
    }

读取、写入和追加JSON文件

我认为如果你创建一个费用类,然后序列化和读取费用对象的集合(即List<Expense>),你会有一个更容易的时间。

public class Expense
{
    public string Name { get; set; }
    public decimal Amount { get; set; }
    public DateTime Due { get; set; }
    public string Recurrence { get; set; }
    public decimal Paid { get; set; }
    public DateTime LastPaid{ get; set; }
}
public class ExpenseCollection : System.Collections.Generic.List<Expense>
{
}

你可以使用内置的JavaScriptSerializer类。

创建JSON的粗略代码:
        ExpenseCollection cExpenses = new ExpenseCollection();
        // ToDo: Fill the expenses collection
        var sJSON = (new System.Web.Script.Serialization.JavaScriptSerializer).Serialize(cExpenses);
        // ToDo: Write sJSON to a file

要阅读它(注意,这可能需要一些调整):

        string sJSON;
        // ToDo: Read the json from a file
        ExpenseCollection cExpenses = (new System.Web.Script.Serialization.JavaScriptSerializer).Deserialize<ExpenseCollection>(sJSON);
        // ToDo: Write sJSON to a file

我相信你的JSON格式不正确,使用@Competent_tech提到的序列化方法它会产生类似的结果:

"Expense": [
  {
    "Name": "something",
    "Amount": "34.90",
    "Due": "28/12/2011",
    "Recurrence": "1 Months",
    "Paid": "0",
    "LastPaid": "01/01/2002"
 }
 {
    "Name": "OneTel Mobile Bill",
    "Amount": "39.90",
    "Due": "28/12/2011",
    "Recurrence": "1 Months",
    "Paid": "0",
    "LastPaid": "01/01/2002"
 }
 {
    "Name": "some other Bill",
    "Amount": "44.90",
    "Due": "28/12/2011",
    "Recurrence": "1 Months",
    "Paid": "0",
    "LastPaid": "01/01/2002"
  }
]