JSON数组到C#字典 [英] JSON array to C# Dictionary

查看:600
本文介绍了JSON数组到C#字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换这样的JSON 数组

How do I convert a JSON array like this

[{"myKey":"myValue"},
{"anotherKey":"anotherValue"}]

到带有json.net或系统类的C#字典.

to a C# Dictionary with json.net or system classes.

json.net可以直接序列化为字典,但仅当您向其提供对象而不是数组时

json.net can serialize directly to a dictionary, but only if you feed it an object, not an array.

推荐答案

也许转换为KeyValuePairs数组会有所帮助

Maybe converting to array of KeyValuePairs will help

using System.Linq;

var list = JsonConvert.DeserializeObject<IEnumerable<KeyValuePair<string, string>>>(jsonContent);
var dictionary = list.ToDictionary(x => x.Key, x => x.Value);

这篇关于JSON数组到C#字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆