【问题标题】:I am getting error deserializing json object我在反序列化 json 对象时遇到错误
【发布时间】:2020-12-07 22:42:40
【问题描述】:

我得到空值反序列化对象。试图删除该 List 关键字,但后来我得到空值。错误-

Cannot deserialize the current JSON object

`

也许您有任何解决方法的想法。 :)

代码:

            client.BaseAddress = new Uri("https://euw1.api.riotgames.com/lol/match/v4/matchlists/by-account/");
            string s = client.GetStringAsync("SRAUZPYTqglRgTjMEzaqY1s-wFMaNZnCjgBHMqQNDnJeJNw?endIndex=10&api_key=RGAPI-7bc6b22c-3ce3-41e6-bfbd-90b1eccb212f").Result;
            var rankInfoList = JsonConvert.DeserializeObject<List<MatchInfo>>(s);

型号:

public class MatchInfo
    {
       
        public string gameId { get; set; }
        public string champion { get; set; }
    }

json:

{
    "matches": [
        {
            "platformId": "EUW1",
            "gameId": 4961339963,
            "champion": 1,
            "queue": 420,
            "season": 13,
            "timestamp": 1607031715226,
            "role": "SOLO",
            "lane": "MID"
        },
        {
            "platformId": "EUW1",
            "gameId": 4961185949,
            "champion": 238,
            "queue": 420,
            "season": 13,
            "timestamp": 1607026682284,
            "role": "SOLO",
            "lane": "MID"
        }
    ],
    "startIndex": 0,
    "endIndex": 2,
    "totalGames": 120
}

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    您需要创建一个具有matches 作为属性的RootObject。匹配项将是一个对象列表 (MatchInfo)

    您的 RootObject 将如下所示,

    public class RootObject
    {
        public List<MatchInfo> matches { get; set; }
        public int startIndex { get; set; }
        public int endIndex { get; set; }
        public int totalGames { get; set; }
    }
    
    public class MatchInfo
    {
        public string platformId { get; set; }
        public long gameId { get; set; }
        public int champion { get; set; }
        public int queue { get; set; }
        public int season { get; set; }
        public long timestamp { get; set; }
        public string role { get; set; }
        public string lane { get; set; }
    }
    

    然后您将使用以下语句进行反序列化

    var rankInfoList = JsonConvert.DeserializeObject<RootObject>(s);
    

    【讨论】:

      【解决方案2】:

      因为 JSON 与模型不匹配。您尝试将其反序列化为的模型是 List&lt;MatchInfo&gt;,但 JSON 不是列表,它是具有包含列表的属性的对象。创建一个包含模型:

      public class MatchInfos
      {
          public List<MatchInfo> matches { get; set; }
      }
      

      并反序列化为:

      var rankInfoList = JsonConvert.DeserializeObject<MatchInfos>(s);
      

      【讨论】:

        【解决方案3】:

        matches 是一个集合 (MatchInfo),但您将根视为一个集合。而是像这样尝试:

        void Main()
        {
            string json = @"{
                ""matches"": [
                    {
                        ""platformId"": ""EUW1"",
                        ""gameId"": 4961339963,
                        ""champion"": 1,
                        ""queue"": 420,
                        ""season"": 13,
                        ""timestamp"": 1607031715226,
                        ""role"": ""SOLO"",
                        ""lane"": ""MID""
                    },
                    {
                        ""platformId"": ""EUW1"",
                        ""gameId"": 4961185949,
                        ""champion"": 238,
                        ""queue"": 420,
                        ""season"": 13,
                        ""timestamp"": 1607026682284,
                        ""role"": ""SOLO"",
                        ""lane"": ""MID""
                    }
                ],
                ""startIndex"": 0,
                ""endIndex"": 2,
                ""totalGames"": 120
            }";
        
        
            var data = JsonConvert.DeserializeAnonymousType(json, new { matches = new[] { new MatchInfo {}}});
            // data.matches is your MatchInfo list.
        }
        
        public class MatchInfo
        {
            public string gameId { get; set; }
            public string champion { get; set; }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-13
          • 1970-01-01
          相关资源
          最近更新 更多