I've generated a C# class from sample JSON output using the Graph Explorer with Contacts (as per below). However, when I pass the serialized object (using JsonConvert.SerializeObject(myclass)) with null values for string array properties to a POST operation to create the Contact I get the following error in the response:
A null value was found for the property named 'BusinessPhones', which
has the expected type 'Collection(Edm.String)[Nullable=False]'. The
expected type 'Collection(Edm.String)[Nullable=False]' does not allow
null values.
(for reference, these are the supported Contact properties: https://msdn.microsoft.com/en-us/office/office365/api/complex-types-for-mail-contacts-calendar#contact)
Here's an example of the raw JSON:
{"AssistantName":null,"Birthday":null,"BusinessAddress":null,"BusinessPhones":null,"Categories":null,"Children":null,"CompanyName":"Fun Toys and Bikes","Department":null,"DisplayName":"Mr. Robert E. Ahlering","EmailAddresses":[{"Address":"robert#funtoys.com","Name":"Robert E. Ahlering (robert#funtoys.com)"}],"FileAs":null,"Generation":null,"GivenName":"Robert","HomeAddress":null,"Initials":null,"JobTitle":"Owner","Manager":null,"MiddleName":null,"MobilePhone1":null,"NickName":null,"OfficeLocation":null,"OtherAddress":null,"PersonalNotes":null,"Profession":null,"SpouseName":null,"Surname":"Ahlering","Title":null,"YomiCompanyName":null,"YomiGivenName":null,"YomiSurname":null}
I believe the correct formatting for a null BusinessPhones value (to pick one of the string arrays) should be "BusinessPhones":[], and not "BusinessPhones:null".
So how do I implement string array/collection properties in the class to accommodate this?
public partial class OutlookContact
{
public partial class Businessaddress
{
public object City { get; set; }
public object CountryOrRegion { get; set; }
public object PostalCode { get; set; }
public object State { get; set; }
public object Street { get; set; }
}
public partial class Emailaddress
{
public string Address { get; set; }
public string Name { get; set; }
}
public partial class Homeaddress
{
public string City { get; set; }
public object CountryOrRegion { get; set; }
public string PostalCode { get; set; }
public string State { get; set; }
public string Street { get; set; }
}
public partial class Otheraddress
{
public object City { get; set; }
public object CountryOrRegion { get; set; }
public object PostalCode { get; set; }
public object State { get; set; }
public object Street { get; set; }
}
public partial class Rootobject
{
public object AssistantName { get; set; }
public DateTime? Birthday { get; set; }
public Businessaddress BusinessAddress { get; set; }
public string[] BusinessPhones { get; set; }
public object[] Categories { get; set; }
public object[] Children { get; set; }
public object CompanyName { get; set; }
public string Department { get; set; }
public string DisplayName { get; set; }
public Emailaddress[] EmailAddresses { get; set; }
public string FileAs { get; set; }
public object Generation { get; set; }
public string GivenName { get; set; }
public Homeaddress HomeAddress { get; set; }
public object Initials { get; set; }
public string JobTitle { get; set; }
public object Manager { get; set; }
public object MiddleName { get; set; }
public object MobilePhone1 { get; set; }
public object NickName { get; set; }
public string OfficeLocation { get; set; }
public Otheraddress OtherAddress { get; set; }
public object PersonalNotes { get; set; }
public object Profession { get; set; }
public object SpouseName { get; set; }
public string Surname { get; set; }
public object Title { get; set; }
public object YomiCompanyName { get; set; }
public object YomiGivenName { get; set; }
public object YomiSurname { get; set; }
}
}
You need to initialize BusinessPhones property to an empty array in the constructor of RootObject class as following.
public partial class RootObject
{
public RootObject()
{
this.BusinessPhones = new string[0];
}
// rest of the class...
}
And if that's not possible, you need to make sure that the BusinessPgones is initialized to empty array before you serialize it to JSON string.
var rootObject = new RootObject();
rootObject.BusinessPhones = new string[];
// set other properties of rootObject
//Serialize rootObject to JSON
// Call rest API.
This should resolve your issue.
From the error null collection is not allowed. You should be able to use any empty collection instead:
"BusinessPhones": []
I'm deserializing from a json file , which has the following fields :
{ 'IdTechnician': '4', 'DescTechnician': 'Surname Name', 'LoginTechnician': 'username', 'TypeTechnician': '1', 'TelephoneTechnician': '+123456789', 'SignatureTechnician': '????\\u0000\\u0010JFIF\\u0000\\u0001\\u0002\\u0001\\u0001,\\u0001,\\u0000\\u0000??\\u0000\\u000eAdobe\\u0000d\\u0000\\u0000\\u0000\\u0000\\u0001??\\t?Exif\\u0000\\u0000MM\\u0000*\\u0000\\u0000\\u0000\\b\\u0000\\u0007\\u0001\\u0012\\u0000\\u0003\\u0000\\u0000\\u0000\\u0001\\u0000\\u0001\\u0000\\u0000\\u0001\\u001a\\u0000\\u0005\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000b\\u0001\\u001b\\u0000\\u0005\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000j\\u0001(\\u0000\\u0003\\u0000\\u0000\\u0000\\u0001\\u0000\\u0002\\u0000\\u0000\\u00011\\u0000\\u0002\\u0000\\u0000\\u0000\\u001c\\u0000\\u0000\\u0000r\\u00012\\u0000\\u0002\\u0000\\u0000\\u0000\\u0014\\u0000\\u0000\\u0000??i\\u0000\\u0004\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000?\\u0001,\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0001,\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000Adobe Photoshop CS4 Windows\\u00002011:03:09 17:10:23\\u0000\\u0000\\u0003?\\u0001\\u0000\\u0003\\u0000\\u0000\\u0000\\u0001??\\u0000\\u0000?\\u0002\\u0000\\u0004\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000??\\u0003\\u0000\\u0004\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000-\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0006\\u0001\\u0003\\u0000\\u0003\\u0000\\u0000\\u0000\\u0001\\u0000\\u0006\\u0000\\u0000\\u0001\\u001a\\u0000\\u0005\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0001\\u001c\\u0001\\u001b\\u0000\\u0005\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0001$\\u0001(\\u0000\\u0003\\u0000\\u0000\\u0000\\u0001\\u0000\\u0002\\u0000\\u0000\\u0002\\u0001\\u0000\\u0004\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0001,\\u0002\\u0002\\u0000\\u0004\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\b^\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000H\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000H\\u0000\\u0000\\u0000\\u0001????\\u0000\\u0010JFIF\\u0000\\u0001\\u0002\\u0000\\u0000H\\u0000H\\u0000\\u0000??\\u0000\\fAdobe_CM\\u0000\\u0002??\\u0000\\u000eAdobe\\u0000d?\\u0000\\u0000\\u0000\\u0001??\\u0000?\\u0000\\f\\b\\b\\b\\t\\b\\f\\t\\t\\f\\u0011\\u000b\\n\\u000b\\u0011\\u0015\\u000f\\f\\f\\u000f\\u0015\\u0018\\u0013\\u0013\\u0015\\u0013\\u0013\\u0018\\u0011\\f\\f\\f\\f\\f\\f\\u0011\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\u0001\\r\\u000b\\u000b\\r\\u000e\\r\\u0010\\u000e\\u000e\\u0010\\u0014\\u000e\\u000e\\u000e\\u0014\\u0014\\u000e\\u000e\\u000e\\u000e\\u0014\\u0011\\f\\f\\f\\f\\f\\u0011\\u0011\\f\\f\\f\\f\\f\\f\\u0011\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f??\\u0000\\u0011\\b\\u0000$\\u0000?\\u0003\\u0001\\\\u0000\\u0002\\u0011\\u0001\\u0003\\u0011\\u0001??\\u0000\\u0004\\u0000\\n??\\u0001?\\u0000\\u0000\\u0001\\u0005\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0003\\u0000\\u0001\\u0002\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\u0001\\u0000\\u0001\\u0005\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\u0010\\u0000\\u0001\\u0004\\u0001\\u0003\\u0002\\u0004\\u0002\\u0005\\u0007\\u0006\\b\\u0005\\u0003\\f3\\u0001\\u0000\\u0002\\u0011\\u0003\\u0004!\\u00121\\u0005AQa\\u0013\\q?2\\u0006\\u0014???B#$\\u0015R?b34r??C\\u0007%?S???cs5\\u0016???&D?TdE??t6\\u0017?U?e?????u??F'???????????????Vfv????????7GWgw????????\\u0011\\u0000\\u0002\\u0002\\u0001\\u0002\\u0004\\u0004\\u0003\\u0004\\u0005\\u0006\\u0007\\u0007\\u0006\\u00055\\u0001\\u0000\\u0002\\u0011\\u0003!1\\u0012\\u0004AQaq\\\\u0013\\u00052??\\u0014??B#?R??3$b?r??CS\\u0015cs4?%\\u0006\\u0016???\\u0007&5??D?T?\\u0017dEU6te??????u??F???????????????Vfv????????'7GWgw???????\\u0000\\f\\u0003\\u0001\\u0000\\u0002\\u0011\\u0003\\u0011\\u0000?\\u0000?E[??;\\u0013\\u0006??????Z?0]?????????3?m?t?f}??/????????????:?????\\u0000K?\\u0000\\u0017_?\\u0011\\u0002?????F5.??\\u001b]L??05???<l??qq?}4???C^??\\u0000\\u0011???\\u0000?{?\\u0000??oC?e?2???+7\\r??&??[?>??z~?^?[-?}??V:??44PQ \\t&\\u0002`A\\u0012\\f??\\n?X???j\\u001c???????\\u0016???w?1??c?????n,????\\u0002\\u0012?-M??I\\u0005)$?IJU?3YM???n?sK?C#v????;k*g??V\\u001e????!?h?\\u0013?\\u0003RVN\\u0006\\u0016]????h??h????\\u0000\\u001fv;\\u0019??;\\u001a?z_???z^?!N?6Krk?\\u001a?0?\\u0016=???sN?5?-EB???\\u001a??^????\\u001d?q's??\\u001f????z*\\nRI$???I???nq\\r\\u0003?t\\t)t?+??G?E??4?\\u000b}F????,^???p?\\u0019???\\u0013\\'?\\u00034?\\u0000?s\\u0005_???=?????N??y????????\\\\?y?io?????w;??c?\\u0000?~????j?X??f??c?1???~?I?????\\u001c??,?~S???\\u0000???GcfU?fN3j????{ls???{?5?1?n???z~??}??\\u001e]??????????K^????\\u001b??????k?~E??{??P?l???\\t>?P?p?\\u0005?\\u0000??\\u0000?????\\u0000a\\u00133??\\u0002?sl??,?m/u??\\\\??????w?w?????r????r???????,]_??\\u0003?o?\\r???cY?e?~?u?Mv^5?\\u001a?j?c\\u0000?\\u0017?m?\\u00007eh???\\u0000*S??:?3?sZ?T???n\\u0000\\u001a?w?{\\u0018????????B??J?\\u0000?10?v3?w??????$??V??=??W?mX?Q???????;k=,Ue??X??f&?B?=????????F???g?[n^NmtdWS????????h?????\\u001a=OK??????}???\\u0011?hv?[?beU?Cn?\\u0010\\f?1?\\u001c?4???\\u001b??V?\\u0000c??????\\u001bE???\\u001d?\\u000688?????EsN????uXT_????vU-?O?X{\\u001df\\u0007?\\u001f??n???>?]?????=\\u001d??Tu*\\u001d??n\\u0016\\u000eP??sK??YT??k???{(k~????????\\u0003\\u001e???H\\u0010D?A???C?g???b?<????<~??,?R?????\\u0000??????\\rh\\rh\\u0010\\u0000?\\u0000\\u0013i-.??7?f?\\u0018>??Gii\\u0005??V?e\\u0014\\n??6??+?\\u0012\\u0004?=??~???Oclc?x\\u000ec?k?u\\u0004\\u001d\\u001c????D?b??k????W?V????????hs[^???c???g??\\u0000??\\u0014tS?????ev5????i\\u0004???\\u0003??m???\\\\<zQ?|?c??\\u0015????????v\\u0015\\u0015T?dd>?c?,??\\u001b\\u001d?6?[n???Z?G\\u001f?\\'??????~;???s?ln77y8?WX;??\\u001a?1?no???Uz??J?n????\\\\`\\rI<\\u0000?r???W?\\u0017d??5\\u000e\\ro??\\u00077??b?K?\\u0019?\\u0017?? ?????h,ap\\u0011?}?????????tG?ul??????-}/c\\u001c???\\u001f??\\u0014?kk~U???}??c???[?\\'??L?Z???T?\\u00167??ac\\u001cX????]??ev;??6????Q?W??>????#7WV???\\b??O??????\\u0016>???m?\\u001c?;!???????q?\\u0011?\\u001a?+??\\u0001??L????\\u001a?S?\\u0015h????X??,?e~??????h?\\u000e?????????\\u0000????Z\\u0003?\\u0015^\\r?*0[K-?????\\u0005????\\u001f??mk??????t??Z???h??k???gc\\bkv???#??]g??V?\\u000e????D???$??????$??????$?????? ??????\\'??????$??????$??????$??????$??????$?????\\u000e8Photoshop 3.0\\u00008BIM\\u0004%\\u0000\\u0000\\u0000\\u0000\\u0000\\u0010\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u00008BIM\\u0003?\\u0000\\u0000\\u0000\\u0000\\u0000\\u0010\\u0001,\\u0000\\u0000\\u0000\\u0001\\u0000\\u0002\\u0001,\\u0000\\u0000\\u0000\\u0001\\u0000\\u00028BIM\\u0004&\\u0000\\u0000\\u0000\\u0000\\u0000\\u000e\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000??\\u0000\\u00008BIM\\u0004\\r\\u0000\\u0000\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u001e8BIM\\u0004\\u0019\\u0000\\u0000\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u001e8BIM\\u0003?\\u0000\\u0000\\u0000\\u0000\\u0000\\t\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u00008BIM'\\u0010\\u0000\\u0000\\u0000\\u0000\\u0000\\n\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u00028BIM\\u0003?\\u0000\\u0000\\u0000\\u0000\\u0000H\\u0000/ff\\u0000\\u0001\\u0000lff\\u0000\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000/ff\\u0000\\u0001\\u0000???\\u0000\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u00002\\u0000\\u0000\\u0000\\u0001\\u0000Z\\u0000\\u0000\\u0000\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u00005\\u0000\\u0000\\u0000\\u0001\\u0000-\\u0000\\u0000\\u0000\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u00018BIM\\u0003?\\u0000\\u0000\\u0000\\u0000\\u0000p\\u0000\\u0000??????????????????????\\u0003?\\u0000\\u0000\\u0000\\u0000??????????????????????\\u0003?\\u0000\\u0000\\u0000\\u0000??????????????????????\\u0003?\\u0000\\u0000\\u0000\\u0000??????????????????????\\u0003?\\u0000\\u00008BIM\\u0004\\b\\u0000\\u0000\\u0000\\u0000\\u0000\\u0010\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0002#\\u0000\\u0000\\u0002#\\u0000\\u0000\\u0000\\u00008BIM\\u0004\\u001e\\u0000\\u0000\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u00008BIM\\u0004\\u001a\\u0000\\u0000\\u0000\\u0000\\u0003O\\u0000\\u0000\\u0000\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000-\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000\\r\\u0000c\\u0000o\\u0000r\\u0000s\\u0000i\\u0000n\\u0000i\\u0000a\\u0000n\\u0000d\\u0000r\\u0000e\\u0000a\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000-\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0010\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000null\\u0000\\u0000\\u0000\\u0002\\u0000\\u0000\\u0000\\u0006boundsObjc\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Rct1\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u0000Top long\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Leftlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Btomlong\\u0000\\u0000\\u0000-\\u0000\\u0000\\u0000\\u0000Rghtlong\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000\\u0006slicesVlLs\\u0000\\u0000\\u0000\\u0001Objc\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0005slice\\u0000\\u0000\\u0000\\u0012\\u0000\\u0000\\u0000\\u0007sliceIDlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0007groupIDlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0006originenum\\u0000\\u0000\\u0000\\fESliceOrigin\\u0000\\u0000\\u0000\\rautoGenerated\\u0000\\u0000\\u0000\\u0000Typeenum\\u0000\\u0000\\u0000\\nESliceType\\u0000\\u0000\\u0000\\u0000Img \\u0000\\u0000\\u0000\\u0006boundsObjc\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Rct1\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u0000Top long\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Leftlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Btomlong\\u0000\\u0000\\u0000-\\u0000\\u0000\\u0000\\u0000Rghtlong\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000\\u0003urlTEXT\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000nullTEXT\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000MsgeTEXT\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0006altTagTEXT\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u000ecellTextIsHTMLbool\\u0001\\u0000\\u0000\\u0000\\bcellTextTEXT\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\thorzAlignenum\\u0000\\u0000\\u0000\\u000fESliceHorzAlign\\u0000\\u0000\\u0000\\u0007default\\u0000\\u0000\\u0000\\tvertAlignenum\\u0000\\u0000\\u0000\\u000fESliceVertAlign\\u0000\\u0000\\u0000\\u0007default\\u0000\\u0000\\u0000\\u000bbgColorTypeenum\\u0000\\u0000\\u0000\\u0011ESliceBGColorType\\u0000\\u0000\\u0000\\u0000None\\u0000\\u0000\\u0000\\ttopOutsetlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\nleftOutsetlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\fbottomOutsetlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u000brightOutsetlong\\u0000\\u0000\\u0000\\u0000\\u00008BIM\\u0004(\\u0000\\u0000\\u0000\\u0000\\u0000\\f\\u0000\\u0000\\u0000\\u0002??\\u0000\\u0000\\u0000\\u0000\\u0000\\u00008BIM\\u0004\\u0011\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0001\\u00008BIM\\u0004\\u0014\\u0000\\u0000\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u00018BIM\\u0004\\f\\u0000\\u0000\\u0000\\u0000\\bz\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000$\\u0000\\u0000\\u0001?\\u0000\\u0000C?\\u0000\\u0000\\b^\\u0000\\u0018\\u0000\\u0001????\\u0000\\u0010JFIF\\u0000\\u0001\\u0002\\u0000\\u0000H\\u0000H\\u0000\\u0000??\\u0000\\fAdobe_CM\\u0000\\u0002??\\u0000\\u000eAdobe\\u0000d?\\u0000\\u0000\\u0000\\u0001??\\u0000?\\u0000\\f\\b\\b\\b\\t\\b\\f\\t\\t\\f\\u0011\\u000b\\n\\u000b\\u0011\\u0015\\u000f\\f\\f\\u000f\\u0015\\u0018\\u0013\\u0013\\u0015\\u0013\\u0013\\u0018\\u0011\\f\\f\\f\\f\\f\\f\\u0011\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\u0001\\r\\u000b\\u000b\\r\\u000e\\r\\u0010\\u000e\\u000e\\u0010\\u0014\\u000e\\u000e\\u000e\\u0014\\u0014\\u000e\\u000e\\u000e\\u000e\\u0014\\u0011\\f\\f\\f\\f\\f\\u0011\\u0011\\f\\f\\f\\f\\f\\f\\u0011\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f??\\u0000\\u0011\\b\\u0000$\\u0000?\\u0003\\u0001\\\\u0000\\u0002\\u0011\\u0001\\u0003\\u0011\\u0001??\\u0000\\u0004\\u0000\\n??\\u0001?\\u0000\\u0000\\u0001\\u0005\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0003\\u0000\\u0001\\u0002\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\u0001\\u0000\\u0001\\u0005\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\u0010\\u0000\\u0001\\u0004\\u0001\\u0003\\u0002\\u0004\\u0002\\u0005\\u0007\\u0006\\b\\u0005\\u0003\\f3\\u0001\\u0000\\u0002\\u0011\\u0003\\u0004!\\u00121\\u0005AQa\\u0013\\'q?2\\u0006\\u0014???B#$\\u0015R?b34r??C\\u0007%?S???cs5\\u0016???&D?TdE??t6\\u0017?U?e?????u??F'???????????????Vfv????????7GWgw????????\\u0011\\u0000\\u0002\\u0002\\u0001\\u0002\\u0004\\u0004\\u0003\\u0004\\u0005\\u0006\\u0007\\u0007\\u0006\\u00055\\u0001\\u0000\\u0002\\u0011\\u0003!1\\u0012\\u0004AQaq\\\\u0013\\u00052??\\u0014??B#?R??3$b?r??CS\\u0015cs4?%\\u0006\\u0016???\\u0007&5??D?T?\\u0017dEU6te??????u??F???????????????Vfv????????'7GWgw???????\\u0000\\f\\u0003\\u0001\\u0000\\u0002\\u0011\\u0003\\u0011\\u0000?\\u0000?E[??;\\u0013\\u0006??????Z?0]?????????3?m?t?f}??/????????????:?????\\u0000K?\\u0000\\u0017_?\\u0011\\u0002?????F5.??\\u001b]L??05???<l??qq?}4???C^??\\u0000\\u0011???\\u0000?{?\\u0000??oC?e?2???+7\\r??&??[?>??z~?^?[-?}??V:??44PQ \\t&\\u0002`A\\u0012\\f??\\n?X???j\\u001c???????\\u0016???w?1??c?????n,????\\u0002\\u0012?-M??I\\u0005)$?IJU?3YM???n?sK?C#v????;k*g??V\\u001e????!?h?\\u0013?\\u0003RVN\\u0006\\u0016]????h??h????\\u0000\\u001fv;\\u0019??;\\u001a?z_???z^?!N?6Krk?\\u001a?0?\\u0016=???sN?5?-EB???\\u001a??^????\\u001d?q's??\\u001f????z*\\nRI$???I???nq\\r\\u0003?t\\t)t?+??G?E??4?\\u000b}F????,^???p?\\u0019???\\u0013\\'?\\u00034?\\u0000?s\\u0005_???=?????N??y????????\\\\?y?io?????w;??c?\\u0000?~????j?X??f??c?1???~?I?????\\u001c??,?~S???\\u0000???GcfU?fN3j????{ls???{?5?1?n???z~??}??\\u001e]??????????K^????\\u001b??????k?~E??{??P?l???\\t>?P?p?\\u0005?\\u0000??\\u0000?????\\u0000a\\u00133??\\u0002?sl??,?m/u??\\\\??????w?w?????r????r???????,]_??\\u0003?o?\\r???cY?e?~?u?Mv^5?\\u001a?j?c\\u0000?\\u0017?m?\\u00007eh???\\u0000*S??:?3?sZ?T???n\\u0000\\u001a?w?{\\u0018????????B??J?\\u0000?10?v3?w??????$??V??=??W?mX?Q???????;k=,Ue??X??f&?B?=????????F???g?[n^NmtdWS????????h?????\\u001a=OK??????}???\\u0011?hv?[?beU?Cn?\\u0010\\f?1?\\u001c?4???\\u001b??V?\\u0000c??????\\u001bE???\\u001d?\\u000688?????EsN????uXT_????vU-?O?X{\\u001df\\u0007?\\u001f??n???>?]?????=\\u001d??Tu*\\u001d??n\\u0016\\u000eP??sK??YT??k???{(k~????????\\u0003\\u001e???H\\u0010D?A???C?g???b?<????<~??,?R?????\\u0000??????\\rh\\rh\\u0010\\u0000?\\u0000\\u0013i-.??7?f?\\u0018>??Gii\\u0005??V?e\\u0014\\n??6??+?\\u0012\\u0004?=??~???Oclc?x\\u000ec?k?u\\u0004\\u001d\\u001c????D?b??k????W?V????????hs[^???c???g??\\u0000??\\u0014tS?????ev5????i\\u0004???\\u0003??m???\\\\<zQ?|?c??\\u0015????????v\\u0015\\u0015T?dd>?c?,??\\u001b\\u001d?6?[n???Z?G\\u001f?\\'??????~;???s?ln77y8?WX;??\\u001a?1?no???Uz??J?n????\\\\`\\rI<\\u0000?r???W?\\u0017d??5\\u000e\\ro??\\u00077??b?K?\\u0019?\\u0017?? ?????h,ap\\u0011?}?????????tG?ul??????-}/c\\u001c???\\u001f??\\u0014?kk~U???}??c???[?\\'??L?Z???T?\\u00167??ac\\u001cX????]??ev;??6????Q?W??>????#7WV???\\b??O??????\\u0016>???m?\\u001c?;!???????q?\\u0011?\\u001a?+??\\u0001??L????\\u001a?S?\\u0015h????X??,?e~??????h?\\u000e?????????\\u0000????Z\\u0003?\\u0015^\\r?*0[K-?????\\u0005????\\u001f??mk??????t??Z???h??k???gc\\bkv???#??]g??V?\\u000e????D???$??????$??????$?????? ??????\\'??????$??????$??????$??????$??????$???8BIM\\u0004!\\u0000\\u0000\\u0000\\u0000\\u0000U\\u0000\\u0000\\u0000\\u0001\\u0001\\u0000\\u0000\\u0000\\u000f\\u0000A\\u0000d\\u0000o\\u0000b\\u0000e\\u0000 \\u0000P\\u0000h\\u0000o\\u0000t\\u0000o\\u0000s\\u0000h\\u0000o\\u0000p\\u0000\\u0000\\u0000\\u0013\\u0000A\\u0000d\\u0000o\\u0000b\\u0000e\\u0000 \\u0000P\\u0000h\\u0000o\\u0000t\\u0000o\\u0000s\\u0000h\\u0000o\\u0000p\\u0000 \\u0000C\\u0000S\\u00004\\u0000\\u0000\\u0000\\u0001\\u00008BIM\\u0004\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u0007\\u0000\\b\\u0000\\u0000\\u0000\\u0001\\u0001\\u0000??\\u0010Chttp://ns.adobe.com/xap/1.0/\\u0000<?xpacket begin=\\'???\\' id=\\'W5M0MpCehiHzreSzNTczkc9d\\'?>\\r\\n<x:xmpmeta xmlns:x=\\'adobe:ns:meta/\\' x:xmptk=\\'Adobe XMP Core 4.2.2-c063 53.352624, 2008/07/30-18:12:18 \\'>\\r\\n\\t<rdf:RDF xmlns:rdf=\\'http://www.w3.org/1999/02/22-rdf-syntax-ns#\\'>\\r\\n\\t\\t<rdf:Description rdf:about=\\\\' xmlns:xmp=\\'http://ns.adobe.com/xap/1.0/\\' xmlns:dc=\\'http://purl.org/dc/elements/1.1/\\' xmlns:photoshop=\\'http://ns.adobe.com/photoshop/1.0/\\' xmlns:xmpMM=\\'http://ns.adobe.com/xap/1.0/mm/\\' xmlns:stEvt=\\'http://ns.adobe.com/xap/1.0/sType/ResourceEvent#\\' xmlns:tiff=\\'http://ns.adobe.com/tiff/1.0/\\' xmlns:exif=\\'http://ns.adobe.com/exif/1.0/\\' xmp:CreatorTool=\\'Adobe Photoshop CS4 Windows\\' xmp:CreateDate=\\'2011-03-09T17:09:27+01:00\\' xmp:ModifyDate=\\'2011-03-09T17:10:23+01:00\\' xmp:MetadataDate=\\'2011-03-09T17:10:23+01:00\\' dc:format=\\'image/jpeg\\' photoshop:ColorMode=\\'3\\' xmpMM:InstanceID=\\'xmp.iid:C525B6FA3E4AE01195AFE2861A054AE9\\' xmpMM:DocumentID=\\'xmp.did:C525B6FA3E4AE01195AFE2861A054AE9\\' xmpMM:OriginalDocumentID=\\'xmp.did:C525B6FA3E4AE01195AFE2861A054AE9\\' tiff:Orientation=\\'1\\' tiff:XResolution=\\'3000000/10000\\' tiff:YResolution=\\'3000000/10000\\' tiff:ResolutionUnit=\\'2\\' tiff:NativeDigest=\\'256,257,258,259,262,274,277,284,530,531,282,283,296,301,318,319,529,532,306,270,271,272,305,315,33432;DE7B977DF6EFF346F11FF6BE697281C5\\' exif:PixelXDimension=\\'200\\' exif:PixelYDimension=\\'45\\' exif:ColorSpace=\\'65535\\' exif:NativeDigest=\\'36864,40960,40961,37121,37122,40962,40963,37510,40964,36867,36868,33434,33437,34850,34852,34855,34856,37377,37378,37379,37380,37381,37382,37383,37384,37385,37386,37396,41483,41484,41486,41487,41488,41492,41493,41495,41728,41729,41730,41985,41986,41987,41988,41989,41990,41991,41992,41993,41994,41995,41996,42016,0,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,22,23,24,25,26,27,28,30;0E127CF2EACEDA719CA574253BD56212\\'>\\r\\n\\t\\t\\t<xmpMM:History>\\r\\n\\t\\t\\t\\t<rdf:Seq>\\r\\n\\t\\t\\t\\t\\t<rdf:li stEvt:action=\\'created\\' stEvt:instanceID=\\'xmp.iid:C525B6FA3E4AE01195AFE2861A054AE9\\' stEvt:when=\\'2011-03-09T17:10:23+01:00\\' stEvt:softwareAgent=\\'Adobe Photoshop CS4 Windows\\'/>\\r\\n\\t\\t\\t\\t</rdf:Seq>\\r\\n\\t\\t\\t</xmpMM:History>\\r\\n\\t\\t</rdf:Description>\\r\\n\\t</rdf:RDF>\\r\\n</x:xmpmeta>\\r\\n \\n o\\u000bP????\\u0006??T??/w?\\u0000\\u0007[???????', 'MailTechnician': 'email#email.it', 'WbsTechnician': 'xyz', 'NumberPincerTechnician': '' }
with the following code
using (StreamReader file = File.OpenText(#"path\\file.json"))
{
JsonSerializer serializer = new JsonSerializer();
tbl_tech tech = (tbl_tech)serializer.Deserialize(file, typeof(tbl_tech));
}
this is the object that i use
public partial class tbl_tech
{
public tbl_tech()
{
this.tbl_odl = new HashSet<tbl_odl>();
this.tbl_pdr = new HashSet<tbl_pdr>();
}
public long IdTechnician { get; set; }
public string DescTechnician { get; set; }
public string LoginTechnician { get; set; }
public string TelephoneTechnician { get; set; }
public string SignatureTechnician { get; set; }
public string MailTechnician { get; set; }
public string WbsTechnician { get; set; }
public string NumberPincerTechnician { get; set; }
public Nullable<long> TypeTechnician { get; set; }
public virtual ICollection<tbl_odl> tbl_odl { get; set; }
public virtual ICollection<tbl_pdr> tbl_pdr { get; set; }
}
and this is the error generate from visual studio.
{"Could not cast or convert from System.String to eStartService.tbl_tech."}
I do not understand is that the fields of the object are perfectly identical to those of my object , so where wrong assignment given to json ? I am that something wrong ? thanks
That json is an array of objects, not an object. See the [ and ] at the beginning/end?
tbl_tech[] tech = (tbl_tech[])serializer.Deserialize(sr, typeof(tbl_tech[]));
If I modify the code like this:
public partial class tbl_tech
{
public tbl_tech()
{
//this.tbl_odl = new HashSet<tbl_odl>();
//this.tbl_pdr = new HashSet<tbl_pdr>();
}
public long IdTechnician { get; set; }
public string DescTechnician { get; set; }
public string LoginTechnician { get; set; }
public string TelephoneTechnician { get; set; }
public string SignatureTechnician { get; set; }
public string MailTechnician { get; set; }
public string WbsTechnician { get; set; }
public string NumberPincerTechnician { get; set; }
public Nullable<long> TypeTechnician { get; set; }
//public virtual ICollection<tbl_odl> tbl_odl { get; set; }
//public virtual ICollection<tbl_pdr> tbl_pdr { get; set; }
}
The deserialization works fine.Becase now your json and your Type are similar.
Try using JSon2csharp when creating your JSon representation.
I removed the "'" from the numbers too:
[{'IdTechnician': 4, 'DescTechnician': 'Surname Name', 'LoginTechnician': 'name', 'TypeTechnician': 1, 'TelephoneTechnician': '+123456789', 'SignatureTechnician': 'signaturepath', 'MailTechnician': 'email#email.com', 'WbsTechnician': 'XYZ', 'NumberPincerTechnician': 1}]
Which gives the following model:
public class RootObject
{
public int IdTechnician { get; set; }
public string DescTechnician { get; set; }
public string LoginTechnician { get; set; }
public int TypeTechnician { get; set; }
public string TelephoneTechnician { get; set; }
public string SignatureTechnician { get; set; }
public string MailTechnician { get; set; }
public string WbsTechnician { get; set; }
public int NumberPincerTechnician { get; set; }
}
You can deserialize it in the following way:
IList<RootObject> result = new List<RootObject>();
using (StreamReader file = File.OpenText(#"path\\file.json"))
{
result = JsonConvert.DeserializeObject<List<RootObject>>(file.ReadToEnd());
}
I haven't done anything with JSON before... so I am probably just missing a step.
Here is an example of the JSON I want to deserialise:
{"item":{"icon":"http://services.runescape.com/m=itemdb_rs/4765_obj_sprite.gif?id=4798","icon_large":"http://services.runescape.com/m=itemdb_rs/4765_obj_big.gif?id=4798","id":4798,"type":"Ammo","typeIcon":"http://www.runescape.com/img/categories/Ammo","name":"Adamant brutal","description":"Blunt adamantite arrow...ouch","current":{"trend":"neutral","price":237},"today":{"trend":"neutral","price":0},"members":"true","day30":{"trend":"positive","change":"+1.0%"},"day90":{"trend":"negative","change":"-0.0%"},"day180":{"trend":"positive","change":"+0.0%"}}}
I put this into "Json 2 C#" and ended up creating this new .cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RSTool.Models
{
public class Current
{
public string trend { get; set; }
public int price { get; set; }
}
public class Today
{
public string trend { get; set; }
public int price { get; set; }
}
public class Day30
{
public string trend { get; set; }
public string change { get; set; }
}
public class Day90
{
public string trend { get; set; }
public string change { get; set; }
}
public class Day180
{
public string trend { get; set; }
public string change { get; set; }
}
public class Item
{
public string icon { get; set; }
public string icon_large { get; set; }
public int id { get; set; }
public string type { get; set; }
public string typeIcon { get; set; }
public string name { get; set; }
public string description { get; set; }
public Current current { get; set; }
public Today today { get; set; }
public string members { get; set; }
public Day30 day30 { get; set; }
public Day90 day90 { get; set; }
public Day180 day180 { get; set; }
}
public class RootObject
{
public Item item { get; set; }
}
}
So, I have the class. I can retrieve the JSON from its location as a string, but I really have no idea how to deserialise it... I have installed Newtonsoft.Json and have tried using PopulateObject and also Deserializer but not with any luck...
Assuming that my JSON is stored as a string called "json", how would I go about storing that query and then retrieving the item name, for example?
Use:
var deserialized = JsonConvert.DeserializeObject<RootObject>(json);
I just tested this successfully given the code you supplied.
You can then access properties of object as normal:
MessageBox.Show(deserialized.item.name);
So I have been able to get JSON objects for a few things, however this object is quite a bit more complex.
I'm trying to get comments from Reddit.
Here is the method I use:
public async Task<List<string>> GetComments(string currentSubreddit, string topicID)
{
string commentUrl = "http://www.reddit.com/r/" + currentSubreddit + "/comments/" + topicID + "/.json";
List<Comments> commentList = new List<Comments>();
string jsonText = await wc.GetJsonText(commentUrl);
Comments.RootObject deserializeObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Comments.RootObject>(jsonText);
List<string> commentListTest = new List<string>();
//List<string> commentListTest = deserializeObject.data.children[0].data.children;
return commentListTest;
}
This is the GetJsonText method:
public async Task<string> GetJsonText(string url)
{
var request = WebRequest.Create(url);
string text;
request.ContentType = "application/json; charset=utf-8";
var response = (HttpWebResponse)await request.GetResponseAsync();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
return text;
}
And here is a link to the Object: http://pastebin.com/WQ8XXGNA
And a link to the jsonText: http://pastebin.com/7Kh6cA9a
The error returned says this:
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in mscorlib.dll but was not handled in user code
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'JuicyReddit.Comments+RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
I'd appreciate if anybody could help me with figuring out whats wrong with this.
Thanks
There are a few problems with your code actually
public async Task<List<string>> GetComments(string currentSubreddit, string topicID)
You don't need to return a list of string here, u need to return a full object
First rename RootObject in the model to an appropriate name such as "CommentsObject"
So set up your class like so and name it CommentsObject.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YOURNAMESPACE.Comments
{
public class MediaEmbed
{
}
public class SecureMediaEmbed
{
}
public class Data4
{
public int count { get; set; }
public string parent_id { get; set; }
public List<string> children { get; set; }
public string name { get; set; }
public string id { get; set; }
public string subreddit_id { get; set; }
public object banned_by { get; set; }
public string subreddit { get; set; }
public object likes { get; set; }
public object replies { get; set; }
public bool? saved { get; set; }
public int? gilded { get; set; }
public string author { get; set; }
public object approved_by { get; set; }
public string body { get; set; }
public object edited { get; set; }
public object author_flair_css_class { get; set; }
public int? downs { get; set; }
public string body_html { get; set; }
public string link_id { get; set; }
public bool? score_hidden { get; set; }
public double? created { get; set; }
public object author_flair_text { get; set; }
public double? created_utc { get; set; }
public object distinguished { get; set; }
public object num_reports { get; set; }
public int? ups { get; set; }
}
public class Child2
{
public string kind { get; set; }
public Data4 data { get; set; }
}
public class Data3
{
public string modhash { get; set; }
public List<Child2> children { get; set; }
public object after { get; set; }
public object before { get; set; }
}
public class Replies
{
public string kind { get; set; }
public Data3 data { get; set; }
}
public class Data2
{
public string domain { get; set; }
public object banned_by { get; set; }
public MediaEmbed media_embed { get; set; }
public string subreddit { get; set; }
public object selftext_html { get; set; }
public string selftext { get; set; }
public object likes { get; set; }
public object secure_media { get; set; }
public object link_flair_text { get; set; }
public string id { get; set; }
public SecureMediaEmbed secure_media_embed { get; set; }
public bool clicked { get; set; }
public bool stickied { get; set; }
public string author { get; set; }
public object media { get; set; }
public int score { get; set; }
public object approved_by { get; set; }
public bool over_18 { get; set; }
public bool hidden { get; set; }
public string thumbnail { get; set; }
public string subreddit_id { get; set; }
public object edited { get; set; }
public object link_flair_css_class { get; set; }
public object author_flair_css_class { get; set; }
public int downs { get; set; }
public bool saved { get; set; }
public bool is_self { get; set; }
public string permalink { get; set; }
public string name { get; set; }
public double created { get; set; }
public string url { get; set; }
public object author_flair_text { get; set; }
public string title { get; set; }
public double created_utc { get; set; }
public int ups { get; set; }
public int num_comments { get; set; }
public bool visited { get; set; }
public object num_reports { get; set; }
public object distinguished { get; set; }
public Replies replies { get; set; }
public int? gilded { get; set; }
public string parent_id { get; set; }
public string body { get; set; }
public string body_html { get; set; }
public string link_id { get; set; }
public bool? score_hidden { get; set; }
public int? count { get; set; }
public List<string> children { get; set; }
}
public class Child
{
public string kind { get; set; }
public Data2 data { get; set; }
}
public class Data
{
public string modhash { get; set; }
public List<Child> children { get; set; }
public object after { get; set; }
public object before { get; set; }
}
public class CommentsObject
{
public string kind { get; set; }
public Data data { get; set; }
}
}
Make your namespace correct!
Then handle the request and deserialise into a list of commentobjects: (u can use the webclient instead of httpclient if you want, this is just an example)
private HttpClient client;
public async Task<List<CommentsObject>> GetComments()
{
client = new HttpClient();
var response = await client.GetAsync("http://www.reddit.com/r/AskReddit/comments/1ut6xc.json");
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
List<CommentsObject> comments = await JsonConvert.DeserializeObjectAsync<List<CommentsObject>>(json);
return comments;
}
else
{
throw new Exception("Errorhandling message");
}
}
It's not ideal (and not completely an answer but more of a work around) but I created models that mock the reddit response json to make deserialization super easy. I use JsonProperty attributes on my model properties to pretty up the models a bit.
Here are the models
And since my models directly mock the json I can just use json.net's generic deserialize method.
I have the following JSON object coming to me from a web service
{
"room":{
"name":"Thunderdome",
"created_at":"2012/04/15 00:36:27 +0000",
"id":xxxxxxx,
"users":[
{
"type":"Member",
"avatar_url":"url",
"created_at":"2012/02/27 14:11:57 +0000",
"id":1139474,
"email_address":"xxx#xxxxxxx.com",
"admin":false,
"name":"xxxx xxxxx"
},
{
"type":"Member",
etc
I'm using the following line to deserialize:
var room = JsonConvert.DeserializeObject<SingleRoom>(text);
And the following mapping classes
public class SingleRoom
{
public Room Room { get; set; }
}
[DataContract]
public class Room
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
public string Image {
get { return "Assets/campfire.png"; }
}
[DataMember]
public string Topic { get; set; }
[DataMember(Name ="membership_limit")]
public int MembershipLimit { get; set; }
[DataMember]
public bool Full { get; set; }
[DataMember]
public bool Locked { get; set; }
[DataMember(Name = "open_to_guests")]
public bool OpenToGuests { get; set; }
[DataMember(Name = "updated_at")]
public DateTime UpdatedAt { get; set; }
[DataMember(Name = "created_at")]
public DateTime CreatedAt { get; set; }
[DataMember(Name = "active_token_value")]
public string ActiveTokenValue { get; set; }
[DataMember(Name = "Users")]
public List<User> Users { get; set; }
}
[DataContract]
public class User
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember(Name = "email_address")]
public string EmailAddress { get; set; }
[DataMember]
public bool Admin { get; set; }
[DataMember]
public DateTime CreatedAt { get; set; }
[DataMember]
public string Type { get; set; }
[DataMember(Name = "avatar_url")]
public string AvatarUrl { get; set; }
}
The 'Room' object deserializes correctly but the Users property on the Room is always null. I know that a null result is the result of a JSON.NET serialization that couldn't find the matching property. However, I think I have it right? List should match to user. I know the array object users in the JSON doesn't have named children, is that the issue? If so, how do I solve it?
Thanks!
This works.... You can rename them (or use JsonProperty attribute) to use c# style property names
(BTW: Json.Net doesn't require DataMember, DataContract attributes)
var obj = JsonConvert.DeserializeObject<SingleRoom>(json)
public class User
{
public string type { get; set; }
public string avatar_url { get; set; }
public string email_address { get; set; }
public bool admin { get; set; }
public string name { get; set; }
public string created_at { get; set; }
public int id { get; set; }
}
public class Room
{
public string topic { get; set; }
public int membership_limit { get; set; }
public bool locked { get; set; }
public string name { get; set; }
public List<User> users { get; set; }
public bool full { get; set; }
public bool open_to_guests { get; set; }
public string updated_at { get; set; }
public string created_at { get; set; }
public int id { get; set; }
}
public class SingleRoom
{
public Room room { get; set; }
}
PS: You may find that site useful http://json2csharp.com/ .
In my case, I had missing to add the "public" key for the child property.