如何将以下JSON数组转换为IDictionary ?

时间:2021-07-06 19:01:40

Following is the serialized JSON array I want to convert to IDictionary

以下是我想要转换为IDictionary的序列化JSON数组

[
  {
    "8475": 25532
  },
  {
    "243": 521
  },
  {
    "3778": 15891
  },
  {
    "3733": 15713
  }
]

When I tried to use

当我试图使用

JsonConvert.DeserializeObject<IDictionary<string, object>>((string)jarray);

I got an error saying:

我收到一个错误说:

Cannot cast 'jarray' (which has an actual type of 'Newtonsoft.Json.Linq.JArray') to 'string'

无法将'jarray'(其实际类型为'Newtonsoft.Json.Linq.JArray')转换为'string'

The JSON deserializer requires only a string.

JSON反序列化器只需要一个字符串。

2 个解决方案

#1


6  

If you already have the JArray, all you have to do is convert it to a dictionary I guess.

如果您已经拥有JArray,那么您所要做的就是将其转换为字典。

Roughly something like this:

大概是这样的:

IDictionary<string,object> dict = jarray.ToDictionary(k=>((JObject)k).Properties().First().Name, v=> v.Values().First().Value<object>());

Check this for complete code with an example

通过示例检查此完整代码

I think there might be a better way to convert it to a dictionary though. I'll keep looking.

我认为可能有更好的方法将其转换为字典。我会继续寻找。

#2


1  

the JsonConvert.DeserializeObject<T> Method takes a JSON string, in other words a serialized object.
You have a deserialized object, so you'll have to serialize it first, which is actually pointless, considering you have all the information you need right there in the JArray object. If you are aiming just to get the objects from the array as key value pairs, you can do something like this:

JsonConvert.DeserializeObject 方法采用JSON字符串,换句话说是一个序列化对象。你有一个反序列化的对象,所以你必须先将它序列化,这实际上是毫无意义的,因为你在JArray对象中拥有了你需要的所有信息。如果您的目标只是将数组中的对象作为键值对,则可以执行以下操作:

Dictionary<string, object> myDictionary = new Dictionary<string, object>();

foreach (JObject content in jarray.Children<JObject>())
{
    foreach (JProperty prop in content.Properties())
    {
        myDictionary.Add(prop.Name, prop.Value);
    }
}

#1


6  

If you already have the JArray, all you have to do is convert it to a dictionary I guess.

如果您已经拥有JArray,那么您所要做的就是将其转换为字典。

Roughly something like this:

大概是这样的:

IDictionary<string,object> dict = jarray.ToDictionary(k=>((JObject)k).Properties().First().Name, v=> v.Values().First().Value<object>());

Check this for complete code with an example

通过示例检查此完整代码

I think there might be a better way to convert it to a dictionary though. I'll keep looking.

我认为可能有更好的方法将其转换为字典。我会继续寻找。

#2


1  

the JsonConvert.DeserializeObject<T> Method takes a JSON string, in other words a serialized object.
You have a deserialized object, so you'll have to serialize it first, which is actually pointless, considering you have all the information you need right there in the JArray object. If you are aiming just to get the objects from the array as key value pairs, you can do something like this:

JsonConvert.DeserializeObject 方法采用JSON字符串,换句话说是一个序列化对象。你有一个反序列化的对象,所以你必须先将它序列化,这实际上是毫无意义的,因为你在JArray对象中拥有了你需要的所有信息。如果您的目标只是将数组中的对象作为键值对,则可以执行以下操作:

Dictionary<string, object> myDictionary = new Dictionary<string, object>();

foreach (JObject content in jarray.Children<JObject>())
{
    foreach (JProperty prop in content.Properties())
    {
        myDictionary.Add(prop.Name, prop.Value);
    }
}