反序列化的包含字典对象列表 [英] Deserializing a List of Objects that contain a Dictionary

查看:403
本文介绍了反序列化的包含字典对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多的例子,似乎表明我在做什么应该工作,但无论出于何种原因,事实并非如此。我试图反序列化对象的集合,它是一个字典,像这样的一个属性:

I've seen a lot of examples that seem to indicate that what I'm doing should work, but for whatever reason, it doesn't. I'm trying to deserialize a collection of objects, one of the properties of which is a Dictionary, like so:

class Program
{
    static void Main(string[] args)
    {
        var json = "{\"Collection\":[{\"ID\":\"1243\",\"Dictionary\":[{\"Key\":\"color\", \"Value\":\"red\"},{\"Key\":\"size\",\"Value\":\"large\"}]},{\"ID\":\"1243\",\"Dictionary\":[{\"Key\":\"color\", \"Value\":\"blue\"},{\"Key\":\"size\",\"Value\":\"small\"}]}]}";
        //var json = "[{\"ID\":\"1243\",\"Dictionary\":[{\"Key\":\"color\", \"Value\":\"red\"},{\"Key\":\"size\",\"Value\":\"large\"}]},{\"ID\":\"1243\",\"Dictionary\":[{\"Key\":\"color\", \"Value\":\"blue\"},{\"Key\":\"size\",\"Value\":\"small\"}]}]";
        List<MyObject> myObjects = new JavaScriptSerializer().Deserialize<List<MyObject>>(json);
    }
}

[DataContract]
public class MyObject
{
    [DataMember]
    public string ID { get; set; }

    [DataMember]
    public Dictionary<string, string> Dictionary { get; set; }
}

第一个JSON字符串封装在一个对象的整个事情 - 如果我运行一个运行良好,但myObjects只是一个空列表。如果我运行的第二个字符串(没有它被包裹)我得到以下错误:

The first json string encapsulates the whole thing in an object - if I run that one it runs fine but myObjects is just an empty list. If I run the second string (without it being wrapped) I get the following error:

键入System.Collections.Generic.Dictionary`2 [System.String,   mscorlib程序,版本= 4.0.0.0,文化=中立,   公钥= b77a5c561934e089],[System.String,mscorlib程序,   版本= 4.0.0.0,文化=中性公钥= b77a5c561934e089]]   不支持数组的反序列化。

Type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' is not supported for deserialization of an array.

从研究,我已经做到了这一点似乎应该是pretty的直线前进 - 任何人有任何想法,这JSON格式,我应该使用,什么是问题呢?该JSON反序列化就好了,如果我只是做一个对象数组的一个对象,而不是。

From the research I've done this seems like it should be pretty straight forward - anyone have any ideas as to which JSON format I should be using and what is going wrong? The JSON deserializes just fine if I just do one object instead of an array of objects.

推荐答案

呀真,解串器不反序列化dictornary对象especailly如果您有任何复杂类型和日期。该解决方案是使用Newtonsoft.Json使用Jobject进行反序列化,你可以把这个作为一个例子,并尝试。在你的情况,你可以借此为VAR或Jobject

Yeah true, the deserializers dont deserialize the dictornary object especailly if you have any complex types and dates. The solution for that is use Newtonsoft.Json use Jobject to deserialize you can take this as an example and try.. In your case you can take this to var or Jobject

    JArray resources=(JArray)JsonConvert.DeserializeObject(objJson);
                     itemStores = resources.Select(resource => new Resource`enter code here`
                     {
                         SpaceUsed = long.Parse(resource["indexDiskMB"].ToString()),
                         ItemId =resource["id"].ToString(),
                         CountItems =Int32.Parse(resource["numItems"].ToString()),
                         ItemType=resource["type"].ToString()

                     }).ToList();

这篇关于反序列化的包含字典对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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