我正在使用Newtonsoft.Json來讀取json文件。我正在嘗試對json文件進行aysnc調用以讀取其數據,但不幸的是它沒有返回任何內容。我想沒有異步和它完美的作品,下面是我的代碼:異步反序列化調用不在C中工作#
public static async Task<T> LoadAsync<T>(string filePath)
{
// filePath: any json file present locally on the disk
string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");
string fullPath = Path.Combine(basePath, filePath);
using (var stream = File.OpenRead(fullPath))
{
var reader = new StreamReader(stream, Encoding.GetEncoding("iso-8859-1"));
var task = Task.Factory.StartNew(() => JsonConvert.DeserializeObject<T>(reader.ReadToEnd()));
var value = await task;
return value;
}
}
我試圖調試,但調試器在上述方法的「返回值」不來了,我用下面的函數調用上述方法:
private void GetDataFromJson()
{
var value = JsonUtilities.LoadAsync<TaxOffice>(Constant.TAXJSONINPUTFILE);
}
可能是什麼問題?文件存在於我的電腦本地。
如果你的'LoadAsync'方法是'async',那麼你應該等待它。 (這又意味着'GetDataFromJson'應該是'async Task'而不是'void'。) – David