直接将Json.net JSON文件反序列化为C#字典类型? [英] Deserialize Json.net JSON File directly to a C# dictionary type?
本文介绍了直接将Json.net JSON文件反序列化为C#字典类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种使用JSON.NET将文件类型直接反序列化为C#字典类型的方法?
Is there a way using JSON.NET to deserialize a file type directly to a C# dictionary type?
例如:
using (StreamReader file = File.OpenText(myFileName))
{
Dictionary<string, int> mydictionary = new Dictionary<string, string>();
JsonSerializer serializer = new JsonSerializer();
//is there a json.net format to make the next line work
mydictionary = (JSONParameters)serializer.Deserialize(file, typeof(JSONParameters));
//return mydictionary;
}
我说"FILE"不是字典到字典...学习阅读!
I said "FILE" not dictionary to dictionary...Learn to READ!
推荐答案
您可以为此使用JsonConvert.DeserializeObject<>
:
var text = File.ReadAllText(myFileName);
mydictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(text);
这篇关于直接将Json.net JSON文件反序列化为C#字典类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文