[英]JSON C# Parsing Error
This is my JSON 这是我的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. 我正在尝试使用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"]
};
I am getting this error "Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'." 我收到此错误“无法将类型为'Newtonsoft.Json.Linq.JValue'的对象转换为类型为'Newtonsoft.Json.Linq.JObject'。”
i also want to read event_id which is inside results and dividents. 我也想读取在结果和分隔线内的event_id。 Can anyone tell me what i am doing wrong here
谁能告诉我我在做什么错
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 . 当前,您正在获取属性的属性-然后尝试将每个值转换为
JObject
,然后采用that的Description
。 That's one level too deep, as you have: 正如您所拥有的那样,这太深了:
JObject
JObject
为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. 目前尚不清楚为什么要使用
AsJEnumerable()
,但是您所需要的只是根对象的属性,可以通过Properties()
方法轻松获得该对象。 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
我也想读取在结果和分隔线内的event_id
In the data you've given, that's always the same as the value of the EventID
property in the top-level type. 在您提供的数据中,该值始终与顶级类型中
EventID
属性的值相同。 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. 如果您真的想获得分红和结果中的值,则需要弄清楚如何处理这些属性中的多个条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.