1

I have JSON like this:

{
"bookings": {
    "group_id": "abc",
    "name": "Study Rooms",
    "url": "My URL",
    "timeslots": [{
            "room_id": "bcd",
            "room_name": "101",
            "booking_label": "Meeting1",
            "booking_start": "2018-11-30T07:00:00-06:00",
            "booking_end": "2018-11-30T07:30:00-06:00",
            "booking_created": "2018-11-28T11:32:32-06:00"
        }, {
            "room_id": "cde",
            "room_name": "102",
            "booking_label": "Meeting2",
            "booking_start": "2018-11-30T07:30:00-06:00",
            "booking_end": "2018-11-30T08:00:00-06:00",
            "booking_created": "2018-11-28T11:32:32-06:00"
        }, //##AND many more like this##
    ]
}
}

If I try to parse it like this:

var reservations = new { bookings = new { group_id = "", name = "", url="", timeslots = new List<Timeslot>() } };
Newtonsoft.Json.JsonConvert.PopulateObject(jsonResult, reservations);

Only timeslots element gets populated

However, if I declare a model class with properties groop_id, name, url, and timeslots collection, and parse like this:

var reservations = new { bookings = new BookingsModel() };
Newtonsoft.Json.JsonConvert.PopulateObject(jsonResult, reservations);

It works fine.

Question is WHY, and is it possible to parse all elements of JSON without static declaration of the model.

shlasasha
  • 165
  • 1
  • 14
  • Don't have time to go into the why but look at the `Dynamic` object type, https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic – Sean T Dec 05 '18 at 16:24
  • @maccettura I've come accross use cases where I've had to use it, but generally I agree, c# is meant to have static type checking – Sean T Dec 05 '18 at 16:26
  • @SeanT "WHY" is the reason I asked this question :) – shlasasha Dec 05 '18 at 16:29
  • `...and is it possible to parse all elements of JSON without static declaration of the model.` <-- Then why put this? You've clearly posted a 2 part question. I've answered the second part – Sean T Dec 05 '18 at 16:30
  • @shlasasha - you JSON is not well formed: 1) There was an incorrect number of closing braces and brackets at the end. Assuming this to be an error in the question, I fixed it. 2) Numbers with leading zeros like this: `"room_id": 0001` are invalid according to the [JSON standard](https://json.org/). For why see [Why is JSON invalid if an integer begins with 0](https://stackoverflow.com/q/27361565). Json.NET interprets these as [Octal](https://stackoverflow.com/q/29048110) which doesn't help you here, you will need to fix the JSON. – dbc Dec 05 '18 at 17:01
  • @dbc Thank you. I messed up writing this post my real json is a little different, so this is not the reason for my problem. – shlasasha Dec 05 '18 at 17:17

2 Answers2

3

The reason that you are not able to populate your anonymous object is that, in c#, anonymous types are immutable.

Instead, you may use JsonConvert.DeserializeAnonymousType() which will create a new instance of your anonymous type from your existing instance:

var reservations = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(jsonResult, 
        new { bookings = 
            new { group_id = default(string), name = default(string), url=default(string), 
                 timeslots = default(List<Timeslot>) } });      

Sample fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Thank you! actually this works too: `var rr = new { bookings = new { group_id = "", name = "", url="", timeslots = new List() } };` `var a = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonResult, rr.GetType());` – shlasasha Dec 05 '18 at 17:21
0

your asking why
group_id, name, and url arent getting populated?

its because theres no actual value in it.

look at the parameter for JsonSerializerSettings

Mookie
  • 35
  • 9