JSON C# Parsing Error

This is my JSON

{
    "3659639": {
        "EventID": 3659639,
        "RaceNum": 2,
        "Meeting": "Newton Abbot",
        "RaceType": "T",
        "Description": "Attheraces.Com Handicap Chase",
        "Distance": "5300m",
        "TrackCondition": "Good",
        "Weather": "Overcast",
        "Abandoned": 0,
        "SuspendDateTime": "2014-06-17 00:00:42.0000000",
        "OutcomeDateTime": "2014-06-17 00:00:00.0000000",
        "EffectiveRaceDate": "2014-06-16",
        "Status": "Paying",
        "Results": [
            {
                "event_id": 3659639,
                "saddle_number": 11,
                "position": 1,
                "status": "Final"
            },
            {
                "event_id": 3659639,
                "saddle_number": 16,
                "position": 2,
                "status": "Final"
            },
            {
                "event_id": 3659639,
                "saddle_number": 17,
                "position": 3,
                "status": "Final"
            }
        ],
        "Dividends": {
            "0": {
                "event_id": 3659639,
                "source": "NSW",
                "pool_type": "Duet",
                "outcome": "11\/16",
                "pool_value": 79.5,
                "interim_dividend": 11.2,
                "final_dividend": 11.2
            },

            "36": {
                "event_id": 3659639,
                "source": "VIC",
                "pool_type": "Trifecta",
                "outcome": "11\/16\/17",
                "pool_value": 1733,
                "interim_dividend": 2746.2,
                "final_dividend": 2746.2
            },
            "37": {
                "event_id": 3659639,
                "source": "VIC",
                "pool_type": "Win",
                "outcome": "11",
                "pool_value": 2541.06,
                "interim_dividend": 25.5,
                "final_dividend": 25.5
            },
            "RunnerProducts": {
                "11": {
                    "TopeTotePlace": 12,
                    "MidTotePlace": 7.3,
                    "TopeToteWin": 29.8,
                    "MidToteWin": 28,
                    "BestOrSP": 29.8
                },
                "16": {
                    "TopeTotePlace": 2.3,
                    "MidTotePlace": 2
                },
                "17": {
                    "TopeTotePlace": 26.4,
                    "MidTotePlace": 24.2
                }
            }
        }
    },



    "3622800": {
        "EventID": 3622800,
        "RaceNum": 2,
        "Meeting": "Albion Park",
        "RaceType": "H",
        "Description": "Seymour Rising Stars Championship C0 Heat One",
        "Distance": "1660m",
        "TrackCondition": "Good",
        "Weather": "Fine",
        "Abandoned": 0,
        "SuspendDateTime": "2014-06-17 15:09:10.0000000",
        "OutcomeDateTime": "2014-06-17 15:08:00.0000000",
        "EffectiveRaceDate": "2014-06-17",
        "Status": "Closed",
        "Results": [

        ],
        "Dividends": {
            "RunnerProducts": [

            ]
        }
    },



    "3679673": {
        "EventID": 3679673,
        "RaceNum": 6,
        "Meeting": "Thirsk",
        "RaceType": "T",
        "Description": "Market Cross Jewellers Handicap",
        "Distance": "1200m",
        "TrackCondition": null,
        "Weather": null,
        "Abandoned": 0,
        "SuspendDateTime": "2014-06-18 02:20:00.0000000",
        "OutcomeDateTime": "2014-06-18 02:20:00.0000000",
        "EffectiveRaceDate": "2014-06-17",
        "Status": "Open",
        "Results": [

        ],
        "Dividends": {
            "RunnerProducts": [

            ]
        }
    }
}

I am trying to parse this using JSON.Net and i have tried this code.

var obj = JObject.Parse(json);
var query =
    from JProperty ev in obj.AsJEnumerable()
    from JProperty evid in ev.Value.AsJEnumerable()
    let value = (JObject)evid.Value
    select new
    {
        Description = (string)value["Description"]

    };

I am getting this error "Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'."

i also want to read event_id which is inside results and dividents. Can anyone tell me what i am doing wrong here

Jon Skeet
people
quotationmark

Currently, you're getting the properties of the properties - and then trying to cast each of the values to JObject, and then taking the Description of that. That's one level too deep, as you have:

  • The root object
  • Each property of the root object, which genuinely has an object as its value
  • Each property of each of those values... most of which are just string properties, so the value can't be cast to JObject

It's not clear why you're using AsJEnumerable() at all, but all you need is the properties of the root object, which is easily obtained with the Properties() method. Likewise it's not clear why you're using an anonymous type, rather than just getting a sequence of strings. This works fine:

var query =
    from ev in obj.Properties()
    select (string) ev.Value["Description"];

Or without the query syntax:

var query = obj.Properties.Select(ev => (string) ev.Value["Description"]);

Next:

i also want to read event_id which is inside results and dividents

In the data you've given, that's always the same as the value of the EventID property in the top-level type. So you'd be better off with:

var query =
    from ev in obj.Properties()
    select new { Description = (string) ev.Value["Description"],
                 Id = (string) ev.Value["EventID"] };

If you really want to get at the values in the dividends and results, you'll need to work out how you'll handle the multiple entries in those properties.

people

See more on this question at Stackoverflow