尝试反序列化PlayerStats时获取JsonSerializationException

时间:2018-07-03 18:09:00

标签: c# serialization json.net

我想从JSON插入类数据,但是得到了Newtonsoft.Json.JsonSerializationException。 我认为PlayerStats类很好,而且我不知道为什么它不起作用。 我可以下载JSON并将其打印到控制台,但是当我尝试反序列化时,我的代码停止工作。我试图添加设置以反序列化,但是仍然无法正常工作。

这是我的代码:

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Net;

namespace DiscordBot
{
    class Osu
    {
        public string _nickname;
        private string _key = "key";

        public class PlayerStats
        {
            [JsonProperty("user_id")]
            public int UserId { get; set; }

            [JsonProperty("username")]
            public string UserName { get; set; }

            [JsonProperty("count300")]
            public int Count300 { get; set; }

            [JsonProperty("count100")]
            public int Count100 { get; set; }

            [JsonProperty("count50")]
            public int Count50 { get; set; }

            [JsonProperty("playcount")]
            public int PlayCount { get; set; }

            [JsonProperty("ranked_score")]
            public long RankedScore { get; set; }

            [JsonProperty("total_score")]
            public long TotalScore { get; set; }

            [JsonProperty("pp_rank")]
            public int PpRank { get; set; }

            [JsonProperty("level")]
            public double Level { get; set; }

            [JsonProperty("pp_raw")]
            public double RawPp { get; set; }

            [JsonProperty("accuracy")]
            public double Accuracy { get; set; }

            [JsonProperty("count_rank_ss")]
            public int CountRankSs { get; set; }

            [JsonProperty("count_rank_ssh")]
            public int CoundRankSsh { get; set; }

            [JsonProperty("count_rank_s")]
            public int CountRankS { get; set; }

            [JsonProperty("count_rank_sh")]
            public int CountRankSh { get; set; }

            [JsonProperty("count_rank_a")]
            public int CountRankA { get; set; }

            [JsonProperty("country")]
            public string Country { get; set; }

            [JsonProperty("pp_country_rank")]
            public int PpCountryRank { get; set; }
        }

        public PlayerStats GetUserStats()
        {
            string json = string.Empty;
            var result = JsonConvert.DeserializeObject<PlayerStats>(json);
            try
            {
                string url = @"https://osu.ppy.sh/api/get_user";
                using (WebClient wc = new WebClient())
                {
                    wc.QueryString.Add("k", _key);
                    wc.QueryString.Add("u", _nickname);
                    wc.QueryString.Add("m", "0");
                    json = wc.DownloadString(url);
                    Console.WriteLine(json);
                    result = JsonConvert.DeserializeObject<PlayerStats>(json);
                    return result;
                }
            }
            catch (WebException ex)
            {
                Console.WriteLine("Osu Error: " + ex.Status);
            }
            return result;
        }
    }
}

JSON:

[
    {
        "user_id":"10415972"
        ,"username":"iGruby"
        ,"count300":"851431"
        ,"count100":"15449 6"
        ,"count50":"19825"
        ,"playcount":"7129"
        ,"ranked_score":"453511877"
        ,"total_score" :"2735863526"
        ,"pp_rank":"147461"
        ,"level":"74.5611"
        ,"pp_raw":"1642.73"
        ,"accuracy" :"94.46521759033203"
        ,"count_rank_ss":"13"
        ,"count_rank_ssh":"2"
        ,"count_rank_s":"3 6"
        ,"count_rank_sh":"13"
        ,"count_rank_a":"65"
        ,"country":"PL"
        ,"pp_country_rank":"77 20"
        ,"events":[]
    }
]

2 个答案:

答案 0 :(得分:1)

从我已加载您的json和DeserializeObject的文件中引用此测试答案,还要检查我的模型以及我如何DeserializeObject

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string data = File.ReadAllText("D://readjson.txt");
            var obj = JsonConvert.DeserializeObject<List<RootObject>>(data);
        }
    }

    public class RootObject
    {
        public string user_id { get; set; }
        public string username { get; set; }
        public string count300 { get; set; }
        public string count100 { get; set; }
        public string count50 { get; set; }
        public string playcount { get; set; }
        public string ranked_score { get; set; }
        public string total_score { get; set; }
        public string pp_rank { get; set; }
        public string level { get; set; }
        public string pp_raw { get; set; }
        public string accuracy { get; set; }
        public string count_rank_ss { get; set; }
        public string count_rank_ssh { get; set; }
        public string count_rank_s { get; set; }
        public string count_rank_sh { get; set; }
        public string count_rank_a { get; set; }
        public string country { get; set; }
        public string pp_country_rank { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public List<object> events { get; set; }
    }

}

答案 1 :(得分:0)

Pp_country_rank和count_rank_s包含一个空格。试图将其解析为整数。

    "count_rank_s":"3 6"
    ,"count_rank_sh":"13"
    ,"count_rank_a":"65"
    ,"country":"PL"
    ,"pp_country_rank":"77 20"