Which Json deserializer renders IList collections?

前端 未结 3 649
别跟我提以往
别跟我提以往 2021-02-06 18:51

I\'m trying to deserialize json to an object model where the collections are represented as IList types.

The actual deserializing is here:

3条回答
  •  粉色の甜心
    2021-02-06 19:09

    I ended up using the Json.NET lib which has good linq support for custom mapping. This is what my deserializing ended up looking like:

            JArray json = JArray.Parse(
                (new StreamReader(General.GetEmbeddedFile("Contacts.json")).ReadToEnd()));
    
            IList tempContacts = (from c in json
                                           select new Contact
                                           {
                                               ID = (int)c["ID"],
                                               Name = (string)c["Name"],
                                               Details = new LazyList(
                                                   (
                                                       from cd in c["Details"]
                                                       select new ContactDetail
                                                       {
                                                           ID = (int)cd["ID"],
                                                           OrderIndex = (int)cd["OrderIndex"],
                                                           Name = (string)cd["Name"],
                                                           Value = (string)cd["Value"]
                                                       }
                                                    ).AsQueryable()),
                                               Updated = (DateTime)c["Updated"]
                                           }).ToList();
    
            return tempContacts;
    

提交回复
热议问题