我正在做一個簡單的WPF程式:
我如何在Documents檔案夾中的data.json檔案中保存json字串,并使用Json.NET將它們讀回來?
我的代碼:
public void SerializeTest()
{
for (int i = 0; i < 3; i )
{
Product product = new Product()。
product.Name = "Item" i;
productList.Add(product)。
}
json = JsonConvert.SerializeObject(productList, Formatting.Indented)。
}
uj5u.com熱心網友回復:
我猜你要找的是
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)。
你可以使用這個并附加到你想要的檔案上。
json = JsonConvert。 SerializeObject(productList, Formatting.Indented) 。
string path = Environment. GetFolderPath(Environment.SpecialFolder.MyDocuments) "ExampleFolderExampleSubFolderdata.json"。
try
{
File.WriteAllText(path, json)。
}
catch (Exception e)
{
Console.WriteLine(e.Message)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/307043.html
標籤:
上一篇:類'List'沒有一個名為'fromJson'的建構式。嘗試呼叫一個不同的建構式,或者定義一個名為'fromJson'的建構式。
下一篇:If陳述句-TSQL
