无法将“Newtonsoft.Json.Linq.JArray”类型的对象转换为“System.Collections.Generic.List”类型

Posted

技术标签:

【中文标题】无法将“Newtonsoft.Json.Linq.JArray”类型的对象转换为“System.Collections.Generic.List”类型【英文标题】:Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'System.Collections.Generic.List` 【发布时间】:2016-02-10 01:27:00 【问题描述】:

我正在从网络服务读取数据。 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(string json) var myClass = new javascriptSerializer().Deserialize(json); 返回(RunOns)myClass;

公共类 RunOns 公共 PubRunOns runons 得到;放;

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” 有任何想法吗?提前谢谢你。

【问题讨论】:

您的 JSON 是一个列表(数组)而不是对象,所以首先问题是您将一个 JSON 列表(数组)转换为一个类对象。 @ParthTrivedi 如果我将其更改为 RunOns myClass = new JavaScriptSerializer().Deserialize(json); l 仍然报错“MyPropert is not supported for deserialization of an array.” 【参考方案1】:
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());
    

【讨论】:

太棒了!它工作正常,如果它是一个 json 对象,谢谢!【参考方案2】:

可以试试:

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>();
    

【讨论】:

【参考方案3】:

把想要的类型放入

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

而不是强制转换...

【讨论】:

这为我解决了。谢谢阿德尔。

以上是关于无法将“Newtonsoft.Json.Linq.JArray”类型的对象转换为“System.Collections.Generic.List”类型的主要内容,如果未能解决你的问题,请参考以下文章

无法将“Newtonsoft.Json.Linq.JObject”类型的对象强制转换为“Newtonsoft.Json.Linq.JArray”

无法将“Newtonsoft.Json.Linq.JArray”类型的对象转换为“System.Collections.Generic.List”类型

无法将类型'Newtonsoft.Json.Linq.JObject'的对象转换为类型

无法将类型为“Newtonsoft.Json.Linq.JObject”的对象转换为类型“System.Collections.Generic.Dictionary`2[System.String,S

C# 无法访问 Newtonsoft.Json.Linq.JProperty 上的子值

动态 Json - 无法访问 Newtonsoft.Json.Linq.JValue 上的子值