我一直試圖正確解析一段時間的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);
}
謝謝@competent - 我已閱讀s我應該序列化並從List <>中讀取的地方,但不知道從哪裏開始。你能給我一個開始嗎?你知道我有沒有找到的教程? – Jason 2011-12-20 02:18:31
@Jason:用一些想法更新了答案。 – 2011-12-20 02:22:48