【发布时间】:2013-12-28 19:04:53
【问题描述】:
我正在开发 Windows 应用商店应用程序并遇到了问题, 目前我可以像这样读取 .json 文件:
{
"Name": "TestItem2",
"Category": "Undefined",
"Sum": 10.2,
"Date": "28/12/2013"
}
使用此代码:
async public static Task addCostsInDB(string name, double sum, string desc)
{
StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file2 = await storageFolder.CreateFileAsync("costs.json", Windows.Storage.CreationCollisionOption.OpenIfExists);
Costs newCosts = new Costs
{
Name = name,
Sum = sum,
Category = "Undefined",
Date = System.DateTime.Now.Day.ToString() + "/" + System.DateTime.Now.Month.ToString() + "/" + System.DateTime.Now.Year.ToString()
};
var obj = newCosts;
JsonSerializer serializer = new JsonSerializer();
string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
//await FileIO.WriteTextAsync(file, json);
await Windows.Storage.FileIO.WriteTextAsync(file2, json);
ReadJson.ReadJson.addCostsInDB();
bool done = await updateVariables();
while (done == false)
{
}
return;
}
但是,每个文件我只能读取一个对象。我需要能够像这样读取 json 文件:
[
{
"Name": "TestItem1",
"Category": "Undefined",
"Sum": 10.2,
"Date": "28/12/2013"
},
{
"Name": "TestItem2",
"Category": "Undefined",
"Sum": 10.2,
"Date": "28/12/2013"
},
{
"Name": "TestItem3",
"Category": "Undefined",
"Sum": 10.2,
"Date": "28/12/2013"
},
{
"Name": "TestItem4",
"Category": "Undefined",
"Sum": 10.2,
"Date": "28/12/2013"
}
]
谢谢:D
编辑: 这是“成本”类:
public class Costs
{
public string Name { get; set; }
public string Category { get; set; }
public double Sum { get; set; }
public string Date { get; set; }
}
【问题讨论】:
标签: c# .net json xaml windows-store-apps