Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Deserialize JSON with dot in property name

Working with SharePoint REST call, I often have to Deserialize Json string with strange Property name, for example odata.nextLink, with a nice DOT (.) inside.

Here an example...

{
[
{"Id": 28"Title": "Title - F8CDD090-B099-4DDF-AD23-27E08A58A03C""MyDate": "2016-10-25T22:00:00Z",…},
{"Id": 29"Title": "Title - DAD1532A-B82E-4868-AC29-2480252AA148""MyDate": "2016-08-22T22:00:00Z",…},
{"Id": 30"Title": "Title - 654372B6-58A3-4C99-AFF4-E93E49B26DA4""MyDate": "2017-03-24T23:00:00Z",…},
{"Id": 31"Title": "Title - 853D9CA4-E13B-4ADA-BD19-E7DC927C1B2D""MyDate": "2017-05-12T22:00:00Z",…},
{"Id": 32"Title": "Title - A9BF19D1-460D-46EB-8C52-985C70EBC362""MyDate": "2016-06-13T22:00:00Z",…},
{"Id": 33"Title": "Title - 4174059B-CEB3-41DE-A3C2-88CE3591665F""MyDate": "2018-09-29T22:00:00Z",…},
{"Id": 34"Title": "Title - 857A64EA-F32F-4F62-BB2F-EC030DFC6032""MyDate": "2016-12-27T23:00:00Z",…},
{"Id": 35"Title": "Title - 4D57D101-8D62-4B91-AEF3-4DA8027D0D20""MyDate": "2019-06-25T22:00:00Z",…},
{"Id": 36"Title": "Title - FE51E10E-760A-451B-892A-747981B06D36""MyDate": "2017-12-25T23:00:00Z",…},
{"Id": 37"Title": "Title - 02AA8D18-F34E-4114-9226-1ACB62BCF1D4""MyDate": "2014-10-15T22:00:00Z",…}
]
}

When I use JsonConvert.DeserializeObject (thanks to Newtonsoft.Json) I've to create a class which reflects the same propery name of the JSON result.

My mom taught me that I can't create a class property with a dot (.) inside its name.
I can't create a property named odata.nextLink.

Here the solution:
I create a property with a acceptable name, for example OdatanextLink and then I set the Propery Name of the propery to the strange name, using the JsonPropery decoration.

 [JsonProperty(PropertyName = "odata.nextLink")]
            public string OdatanextLink { getset; }

Here my class.

public class Rootobject
        {
            [JsonProperty(PropertyName = "odata.nextLink")]
            public stringOdatanextLink { get; set; }

            [JsonProperty(PropertyName = "value")]
            public Listdynamic> Items { get; set; }

        }





This post first appeared on ZSvipullo, please read the originial post: here

Share the post

Deserialize JSON with dot in property name

×

Subscribe to Zsvipullo

Get updates delivered right to your inbox!

Thank you for your subscription

×