Deserializing a List of Objects that contain a Dictionary

后端 未结 3 492
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 09:06

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

相关标签:
3条回答
  • 2021-01-18 09:45

    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();
    
    0 讨论(0)
  • 2021-01-18 09:51

    Looks like Dictionary<string, string> is not serializable the way you'd expect. I tried List<KeyValuePair<string, string>> instead, and that doesn't seem to work either.

    Only thing I can think of is some ugly stuff that will convert your JSON into a custom type, and then convert it into a dictionary. So using your 2nd JSON example exactly as-is, you can do something like:

    // Inside of MyObject class
    [DataMember]
    public Kvp<string, string>[] Dictionary { get; set; }
    
    public Dictionary<string, string> GetDictionary()
    {
        return Dictionary.ToDictionary(x => x.Key, x => x.Value);
    }
    
    //////////////////
    
    public class Kvp<T1, T2>
    {
        public T1 Key { get; set; }
        public T2 Value { get; set; }
    }
    
    Dictionary<string, string> myDictionary = myObjects[0].GetDictionary();
    

    I'm sure there's a much better way, but this should at least work.

    0 讨论(0)
  • 2021-01-18 09:52

    Obviously a little late to the part for the OP :) but I hit similar today and used the following to solve it:

            //var json = "[{'firstName':'John', 'lastName':'Doe'},{'firstName':'Anna', 'lastName':'Smith'},{'firstName':'Peter','lastName': 'Jones'} ]";
            //var json = "{ 'glossary': { 'title': 'example glossary','GlossDiv': { 'title': 'S','GlossList': { 'GlossEntry': { 'ID': 'SGML','SortAs': 'SGML','GlossTerm': 'Standard Generalized Markup Language','Acronym': 'SGML','Abbrev': 'ISO 8879:1986','GlossDef': { 'para': 'A meta-markup language, used to create markup languages such as DocBook.','GlossSeeAlso': ['GML', 'XML'] },'GlossSee': 'markup' } } } } }";
            var json = "{ 'A': 'A0' , 'B' : { 'B2' : 'B2 - Val', 'B3' : [{'B30' : 'B30 - Val1' ,'B31' : 'B31 - Val1'}]}, 'C': ['C0', 'C1']}";
            var jss = new JavaScriptSerializer();
    
            try
            {
                // Deal with an Object root
                var dict = jss.Deserialize<Dictionary<string, object>>(json);
                //<do stuff>
            }
            catch (InvalidOperationException ioX)
            {
                // Deal with an Array Root
                var dictionaries = jss.Deserialize<Dictionary<string, object>[]>(json);
                foreach (var dict in dictionaries)
                {
                    //<do stuff for each dictionary>
                }
            }
    
    0 讨论(0)
提交回复
热议问题