2011-12-20 81 views
1

我一直試圖正確解析一段時間的json文件,但似乎無法正確解析。我已經嘗試了很多教程和json.net庫,但是似乎無法找到正確的方法來解決這個問題。我的文件都具有相同的格式:從JSON文件中讀取,寫入和追加

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

    } 

回答

4

我認爲你將有一個更容易的時間,如果你創建了一個消費類,然後序列化和讀取集合的費用對象(即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 
+0

謝謝@competent - 我已閱讀s我應該序列化並從List <>中讀取的地方,但不知道從哪裏開始。你能給我一個開始嗎?你知道我有沒有找到的教程? – Jason 2011-12-20 02:18:31

+0

@Jason:用一些想法更新了答案。 – 2011-12-20 02:22:48

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" 
    } 
] 
+0

謝謝,哈哈。但我不知道如何反序列化它呢?我試過代碼示例,我無法使它工作。我不知道如何讓json文件進入列表,然後遍歷列表。 @ Competent的答案不起作用,因爲System.Web.Script.Serialization.JavaScriptSerializer()在Windows Metro風格的應用程序中不可用。能否請你幫忙? 而在 – Jason 2011-12-20 16:22:40

+0

代碼我沒有做過的Windows應用程式新城所以不知道如果我能幫助。我使用@Competent提到的方法。但是,也許第三方庫可以幫助,如:http://json.codeplex.com/ – harag 2011-12-21 08:49:13

+0

好的,謝謝你的幫助。RE:你發佈的鏈接,這就是我目前使用的(正如你可能在我的問題中的代碼示例中已經注意到的那樣) - 不幸的是,作者和讀者在Metro應用程序中不工作,因爲你寫的方式並從Windows Developer Preview中的文件讀取。現在有點不同了。 – Jason 2011-12-22 01:00:09