C# 读取、写入和附加到JSON文件
我已经尝试正确解析一些json文件有一段时间了,但我似乎无法正确解析。我尝试了很多教程和json.net库,但似乎找不到正确的方法。我的所有文件都具有相同的格式: file.json:C# 读取、写入和附加到JSON文件,c#,.net,json,parsing,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/
{
"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字符串/文件,如:
(伪代码)
等等等等
我的第二个问题是:
如何将一个全新的“费用”附加到现有的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);
}
我认为如果您创建一个Expense类,然后序列化并读取一组Expense对象(即
列表
),您的工作会轻松得多
阅读它(注意,这可能需要一些调整):
字符串sJSON;
//ToDo:从文件中读取json
ExpenseCollection cExpenses=(new System.Web.Script.Serialization.JavaScriptSerializer).反序列化(sJSON);
//ToDo:将sJSON写入文件
我认为如果您创建了一个费用类,然后序列化并读取费用对象的集合(即列表
),那么您的工作会轻松得多
阅读它(注意,这可能需要一些调整):
字符串sJSON;
//ToDo:从文件中读取json
ExpenseCollection cExpenses=(new System.Web.Script.Serialization.JavaScriptSerializer).反序列化(sJSON);
//ToDo:将sJSON写入文件
我认为您的JSON格式不正确,使用@Competable\u 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"
}
]
我相信您的JSON格式不正确,使用@competable_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"
}
]
谢谢@Competable-我已经在某个地方读到我应该序列化并从列表中阅读的内容,但不确定从哪里开始。你能给我一个领先的开始吗?你们知道有并没有我还并没有找到的教程?谢谢你们@Competable-我读过一些我应该从列表中序列化和阅读的地方,但不知道从哪里开始。你能给我一个领先的开始吗?你知道我还没有找到什么教程吗?谢谢,哈拉。但我不知道如何反序列化它?我尝试过代码示例,但无法使其正常工作。我不知道如何将json文件放入一个列表,然后遍历该列表@主管的答案无效,因为System.Web.Script.Serialization.JavaScriptSerializer()在Windows Metro样式的应用程序中不可用。你能帮忙吗?代码inI还没有完成Windows Metro应用程序,所以不确定我是否能提供帮助。我使用上面提到的方法。不过,也许第三方库可以提供帮助,比如:好的,谢谢你的帮助。RE:你发布的链接,这就是我目前正在使用的(正如你可能在我的问题中的代码示例中注意到的那样)-但不幸的是,由于你在Windows Developer Preview中写入和读取文件的方式,编写器和读取器在Metro应用程序中不起作用。现在不一样了,谢谢你,哈拉。但我不知道如何反序列化它?我尝试过代码示例,但无法使其正常工作。我不知道如何将json文件放入一个列表,然后遍历该列表@主管的答案无效,因为System.Web.Script.Serialization.JavaScriptSerializer()在Windows Metro样式的应用程序中不可用。你能帮忙吗?代码inI还没有完成Windows Metro应用程序,所以不确定我是否能提供帮助。我使用上面提到的方法。不过,也许第三方库可以提供帮助,比如:好的,谢谢你的帮助。RE:你发布的链接,这就是我目前正在使用的(正如你可能在我的问题中的代码示例中注意到的那样)-但不幸的是,由于你在Windows Developer Preview中写入和读取文件的方式,编写器和读取器在Metro应用程序中不起作用。现在完全不同了。
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
"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"
}
]