JSON c#解析错误



这是我的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": [
            ]
        }
    }
}

我正在尝试使用JSON解析这个。. Net和我已经尝试了这个代码。

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"]
    };

我得到这个错误"无法转换类型'Newtonsoft.Json.Linq的对象"。JValue'输入'Newtonsoft.Json.Linq.JObject'。"

我也想读event_id这是内部的结果和红利。谁能告诉我我在这里做错了什么

目前,您正在获得属性的属性-然后尝试将每个值转换为JObject,然后取Description。这太深了,因为你有:

    根对象
  • 根对象的每个属性,真正以对象作为其值
  • 这些值的每个属性…其中大多数只是字符串属性,所以值不能被强制转换为JObject

您根本不清楚为什么要使用AsJEnumerable(),但您所需要的只是根对象的属性,这很容易通过Properties()方法获得。同样,也不清楚为什么要使用匿名类型,而不是仅仅获取字符串序列。

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

或者不使用查询语法:

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

我还想读取event_id里面的结果和股息

在您给出的数据中,这始终与顶级类型中的EventID属性的值相同。所以你最好写:

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

如果您真的想获得股息和结果中的值,您需要弄清楚如何处理这些属性中的多个条目。

相关内容

  • 没有找到相关文章

最新更新