读取,写入和附加到JSON文件

时间:2011-12-20 01:43:00

标签: c# .net json parsing

我已经尝试过一段时间正确地解析一些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字符串/文件,如: (伪码)

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

    }

2 个答案:

答案 0 :(得分:4)

我认为如果您创建了一个Expense类,然后序列化并读取了一些Expense对象(即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

答案 1 :(得分:0)

我相信您的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"
  }
]