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

喜欢而已 提交于 2019-12-25 07:24:50

问题


Is there a way using JSON.NET to deserialize a file type directly to a C# dictionary type?

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!


回答1:


You can use JsonConvert.DeserializeObject<> for that:

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


来源:https://stackoverflow.com/questions/39539395/deserialize-json-net-json-file-directly-to-a-c-sharp-dictionary-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!