简体   繁体   English

直接将Json.net JSON文件反序列化为C#字典类型?

[英]Deserialize Json.net JSON File directly to a C# dictionary type?

Is there a way using JSON.NET to deserialize a file type directly to a C# dictionary type? 有没有一种使用JSON.NET将文件类型直接反序列化为C#字典类型的方法?

For example: 例如:

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;
    }

I said "FILE" not dictionary to dictionary...Learn to READ! 我说“ FILE”不是字典到字典...学会阅读!

You can use JsonConvert.DeserializeObject<> for that: 您可以JsonConvert.DeserializeObject<>使用JsonConvert.DeserializeObject<>

var text = File.ReadAllText(myFileName);
mydictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM