无法将类型 'Newtonsoft.Json.Linq.JArray' 的对象强制转换为类型"System.Collections.Generic.List"



我正在从Web服务读取数据。json对象看起来像:

var goldString = [  
   {  
      "date":"20151109",
      "day":30,
      "month":"November",
      "year":2015,
      "club":9,
      "clubName":"Flamingo",
      "itw":"XYD",
      "races":{  
         "1":{  
            "race":1,
            "time":"12:20",
            "raceStatus":"Undecided",
            "reference":91,
            "name":"WELCOME TO FLAMINGO PARK MAIDEN PLATE",
            "description":"For Maidens",
            "distance":1000,
            "stake":"R46,000",
            "stakes":"1st: R28,750 | 2nd: R9,200 | 3rd: R4,600 | 4th: R2,300 | 5th: R1,150",
            "surface":"Sand",
            "going":"u0000",
            "goingDescription":"",
            "grade":0,
            "division":"MDNM",
            "group":"G",
            "class":"MAIDEN PLATE",
            "condition":"For Maidens",
            "handicap":false,
            "minWins":0,
            "maxWins":0,
            "minAge":"n/a",
            "maxAge":"n/a",
            "gender":"n/a",
            "runners":[  
            ]
         },
         "2":{  
            "race":2,
            "time":"12:50",
            "raceStatus":"Undecided",
            "reference":92,
            "name":"RACING. IT'S A RUSH PINNACLE STAKES",
            "description":"Open",
            "distance":1800,
            "stake":"R66,000",
            "stakes":"1st: R41,250 | 2nd: R13,200 | 3rd: R6,600 | 4th: R3,300 | 5th: R1,650",
            "surface":"Sand",
            "going":"u0000",
            "goingDescription":"",
            "grade":0,
            "division":"PINN",
            "group":"A",
            "class":"PINNACLE STAKES",
            "condition":"Open",
            "handicap":false,
            "minWins":"n/a",
            "maxWins":"n/a",
            "minAge":"n/a",
            "maxAge":"n/a",
            "gender":"n/a",
            "runners":[  
            ]
         },
         "3":{  
            "race":3,
            "time":"13:20",
            "raceStatus":"Undecided",
            "reference":93,
            "name":"INTERNATIONAL JOCKEYS' CHALLENGE 14 NOVEMBER MAIDEN PLATE",
            "description":"For Maidens",
            "distance":1800,
            "stake":"R46,000",
            "stakes":"1st: R28,750 | 2nd: R9,200 | 3rd: R4,600 | 4th: R2,300 | 5th: R1,150",
            "surface":"Sand",
            "going":"u0000",
            "goingDescription":"",
            "grade":0,
            "division":"MDNM",
            "group":"G",
            "class":"MAIDEN PLATE",
            "condition":"For Maidens",
            "handicap":false,
            "minWins":0,
            "maxWins":0,
            "minAge":"n/a",
            "maxAge":"n/a",
            "gender":"n/a",
            "runners":[  
            ]
         },
         "4":{  
            "race":4,
            "time":"13:50",
            "raceStatus":"Undecided",
            "reference":94,
            "name":"SOCCER 6 MR 65 HANDICAP",
            "description":"Open",
            "distance":1600,
            "stake":"R43,000",
            "stakes":"1st: R26,875 | 2nd: R8,600 | 3rd: R4,300 | 4th: R2,150 | 5th: R1,075",
            "surface":"Sand",
            "going":"u0000",
            "goingDescription":"",
            "grade":0,
            "division":"MR65",
            "group":"E",
            "class":"MR 65 HANDICAP",
            "condition":"Open",
            "handicap":true,
            "minWins":"n/a",
            "maxWins":"n/a",
            "minAge":"n/a",
            "maxAge":"n/a",
            "gender":"n/a",
            "runners":[  
            ]
         },
         "5":{  
            "race":5,
            "time":"14:20",
            "raceStatus":"Undecided",
            "reference":95,
            "name":"COMPUTAFORM EXPRESS MR 72 HANDICAP",
            "description":"Open",
            "distance":1400,
            "stake":"R46,000",
            "stakes":"1st: R28,750 | 2nd: R9,200 | 3rd: R4,600 | 4th: R2,300 | 5th: R1,150",
            "surface":"Sand",
            "going":"u0000",
            "goingDescription":"",
            "grade":0,
            "division":"MR72",
            "group":"D",
            "class":"MR 72 HANDICAP",
            "condition":"Open",
            "handicap":true,
            "minWins":"n/a",
            "maxWins":"n/a",
            "minAge":"n/a",
            "maxAge":"n/a",
            "gender":"n/a",
            "runners":[  
            ]
         },
         "6":{  
            "race":6,
            "time":"14:55",
            "raceStatus":"Undecided",
            "reference":96,
            "name":"RACING ASSOCIATION FM 67 HANDICAP (F & M)",
            "description":"For Fillies and Mares",
            "distance":1400,
            "stake":"R46,000",
            "stakes":"1st: R28,750 | 2nd: R9,200 | 3rd: R4,600 | 4th: R2,300 | 5th: R1,150",
            "surface":"Sand",
            "going":"u0000",
            "goingDescription":"",
            "grade":0,
            "division":"FM67",
            "group":"E",
            "class":"FM 67 HANDICAP (F & M)",
            "condition":"For Fillies and Mares",
            "handicap":true,
            "minWins":"n/a",
            "maxWins":"n/a",
            "minAge":"n/a",
            "maxAge":"n/a",
            "gender":"female",
            "runners":[  
            ]
         },
         "7":{  
            "race":7,
            "time":"15:25",
            "raceStatus":"Undecided",
            "reference":97,
            "name":"SOCCER GG MR 66 HANDICAP",
            "description":"Open",
            "distance":1200,
            "stake":"R43,000",
            "stakes":"1st: R26,875 | 2nd: R8,600 | 3rd: R4,300 | 4th: R2,150 | 5th: R1,075",
            "surface":"Sand",
            "going":"u0000",
            "goingDescription":"",
            "grade":0,
            "division":"MR66",
            "group":"E",
            "class":"MR 66 HANDICAP",
            "condition":"Open",
            "handicap":true,
            "minWins":"n/a",
            "maxWins":"n/a",
            "minAge":"n/a",
            "maxAge":"n/a",
            "gender":"n/a",
            "runners":[  
            ]
         },
         "8":{  
            "race":8,
            "time":"15:55",
            "raceStatus":"Undecided",
            "reference":98,
            "name":"BOOK A TABLE 011 6811702 MR 84 HANDICAP",
            "description":"Open",
            "distance":1000,
            "stake":"R55,000",
            "stakes":"1st: R34,375 | 2nd: R11,000 | 3rd: R5,500 | 4th: R2,750 | 5th: R1,375",
            "surface":"Sand",
            "going":"u0000",
            "goingDescription":"",
            "grade":0,
            "division":"MR84",
            "group":"C",
            "class":"MR 84 HANDICAP",
            "condition":"Open",
            "handicap":true,
            "minWins":"n/a",
            "maxWins":"n/a",
            "minAge":"n/a",
            "maxAge":"n/a",
            "gender":"n/a",
            "runners":[  
            ]
         },
         "9":{  
            "race":9,
            "time":"16:30",
            "raceStatus":"Undecided",
            "reference":99,
            "name":"NEXT FLAMINGO PARK RACE MEETING IS MONDAY 16 NOV FM 60 HANDICAP (F & M)",
            "description":"For Fillies and Mares",
            "distance":1000,
            "stake":"R43,000",
            "stakes":"1st: R26,875 | 2nd: R8,600 | 3rd: R4,300 | 4th: R2,150 | 5th: R1,075",
            "surface":"Sand",
            "going":"u0000",
            "goingDescription":"",
            "grade":0,
            "division":"FM60",
            "group":"F",
            "class":"FM 60 HANDICAP (F & M)",
            "condition":"For Fillies and Mares",
            "handicap":true,
            "minWins":"n/a",
            "maxWins":"n/a",
            "minAge":"n/a",
            "maxAge":"n/a",
            "gender":"female",
            "runners":[  
            ]
         }
      }
   }
]

public RunOns DeserializeAccountRunOns(字符串json){var myClass=新JavaScriptSerializer()。反序列化(json);return(RunOns)myClass;}

公共类RunOns{公共PubRunOns runon{get;set;}}

public class PubRunOns
{
    public string date { get; set; }
    public int day { get; set; }
    public string month { get; set; }
    public int year { get; set; }
    public int club { get; set; }
    public string clubName { get; set; }
    public string itw { get; set; }
    public Array races { get; set; }        
    public static PubRunOns CreateFromData(string[] data)
    {
        return new PubRunOns
        {
            date = data[0],
            day = Convert.ToInt32(data[1]),
            month = data[2],
            year = Convert.ToInt32(data[3]),
            club = Convert.ToInt32(data[4]),
            clubName = data[5],
            itw = data[6],               
            races = data[7].ToCharArray()
        };
    }        
}
public class Races
{
    public int race { get; set; }
    public string time { get; set; }
    public string raceStatus { get; set; }
    public int reference { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public int distance { get; set; }
    public string stake { get; set; }
    public string stakes { get; set; }
    public string going { get; set; }
    public List<Runner> Runners { get; set; }        
}
public class Runner
{
    public int draw { get; set; }
    public int saddle { get; set; }      
    public string Name { get; set; }
    public string sex { get; set; }
    public string colour { get; set; }
    public int age { get; set; }
}

如果我调用函数:

var test=  DeserializeAccountRunOns(goldString);

我得到一个例外:无法将"System.Object[]"类型的对象强制转换为"MyProject.Class"类型有什么想法吗?提前谢谢。

JArray jsonResponse = JArray.Parse(goldString);
foreach (var item in jsonResponse)
{
    JObject jRaces = (JObject)item["races"];
    foreach (var rItem in jRaces)
    {
        string rItemKey = rItem.Key;
        JObject rItemValueJson = (JObject)rItem.Value;
        Races rowsResult = Newtonsoft.Json.JsonConvert.DeserializeObject<Races>(rItemValueJson.ToString());
    }
}

将所需类型放入<gt;

selectedVals = JsonConvert.DeserializeObject<int[]>(cInDb.PropValue);

而不是铸造。。。

可以尝试使用:

JArray jsonResponse = JArray.Parse(goldString);
foreach (var item in jsonResponse)
{
    foreach (var rItem in jRaces)
    {
        string rItemKey = rItem.Key;
        JObject rItemValueJson = (JObject)rItem.Value;
        Races rowsResult = item.Value<JObject>("races").ToObject<Races>();
    }
}

相关内容

  • 没有找到相关文章

最新更新