JSON字符串到C#中的DataTable对象

箱子

因此,基本上我到处搜索,但找不到适合我的情况的任何东西。

我正在使用《守望先锋》的API(游戏),我想转换一个从网上下载的字符串,并检查它是否具有JSON字符串。

让我向您展示代码:

 < !--language: c# -->
                HttpClient dc = new HttpClient();
                string tag = e.Message.Text.ToString().Substring(7).Replace("#", "-");
                string apiurl = (@"http://api.lootbox.eu/" + "pc/" + "global/" + tag + "/profile");
                HttpResponseMessage datares = await dc.GetAsync(apiurl);
                string finaldata = await datares.Content.ReadAsStringAsync();
                #region PC
                if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "pc/" + "us/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();

                }
               else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "pc/" + "kr/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "pc/" + "eu/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "pc/" + "cn/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                #endregion
                #region XBOX LIVE
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "xbl/" + "us/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "xbl/" + "eu/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "xbl/" + "kr/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "xbl/" + "cn/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "xbl/" + "global/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                #endregion
                #region PSN
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "psn/" + "us/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "psn/" + "global/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "psn/" + "cn/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "psn/" + "eu/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                else if (finaldata.Contains(":404"))
                {
                    apiurl = (@"http://api.lootbox.eu/" + "psn/" + "kr/" + tag + "/profile");
                    datares = await dc.GetAsync(apiurl);
                    finaldata = await datares.Content.ReadAsStringAsync();
                }
                #endregion

                DataTable obj = JsonConvert.DeserializeObject(finaldata);

因此,在这种情况下,示例输出为:

{"data":{"username":"Rezoh","level":305,"games":{"quick":{"wins":"378"},"competitive":{"wins":"82","lost":85,"played":"167"}},"playtime":{"quick":"88 hours","competitive":"36 hours"},"avatar":"https://blzgdapipro-a.akamaihd.net/game/unlocks/0x0250000000000D70.png","competitive":{"rank":"3392","rank_img":"https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-5.png"},"levelFrame":"https://blzgdapipro-a.akamaihd.net/game/playerlevelrewards/0x025000000000092D_Border.png","star":"https://blzgdapipro-a.akamaihd.net/game/playerlevelrewards/0x025000000000092D_Rank.png"}}

现在,我需要将其转换为某种形式的表。

我得到了JSON.Net,但大多数人说在转换之前先设置一个类,

问题是:我在JSON字符串中看到2个“胜利”:和3个“竞争性”:。

因此,在这种情况下,我认为无法上课。我尝试制作一个新的DataTable,如代码的最后一行所示,但它告诉我"Cannot implicitly convert type object to System.Data.DataTable"在使用时JsonConvert.DeserializeObject(finaldata);甚至还尝试.ToString();dates变量以及变量.ToString()

我需要一种适当的方式来显示这些统计信息,因此,例如,我可以显示:

"Stats for user " + obj.Name + ":"
"Wins: " + obj.Wins
"Losses: " + obj.Losses
"Rank: " + obj.Rank

在这种情况下,没有任何在线解决方案可以帮助我。

编辑:

此解决方案对我也不起作用:

将JSON字符串转换为数据表?或此嵌套的Json字符串到DataTable

这也不是:

  var token = JToken.Parse(finaldata);

                if (token.Type == JTokenType.Object)
                    token = new JArray(token);

                var a = token.ToObject<DataTable>();
lo

您可以使用他们说的课程。我使用了http://json2csharp.com/,但是VS也可以做到。

您可以在这里尝试:https : //dotnetfiddle.net/iaIvOn

using System;
using Newtonsoft.Json;

public class Program
{
    public void Main()
    {
        var json = @"{""data"":{""username"":""Rezoh"",""level"":305,""games"":{""quick"":{""wins"":""378""},""competitive"":{""wins"":""82"",""lost"":85,""played"":""167""}},""playtime"":{""quick"":""88 hours"",""competitive"":""36 hours""},""avatar"":""https://blzgdapipro-a.akamaihd.net/game/unlocks/0x0250000000000D70.png"",""competitive"":{""rank"":""3392"",""rank_img"":""https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-5.png""},""levelFrame"":""https://blzgdapipro-a.akamaihd.net/game/playerlevelrewards/0x025000000000092D_Border.png"",""star"":""https://blzgdapipro-a.akamaihd.net/game/playerlevelrewards/0x025000000000092D_Rank.png""}}";

        // read the doc: http://www.newtonsoft.com/json
        var rootObject = JsonConvert.DeserializeObject<RootObject>(json);

        Console.WriteLine("Stats for user " + rootObject.Data.Username + ":");
        Console.WriteLine("Wins: " + rootObject.Data.Games.Competitive.Wins);
        Console.WriteLine("Losses: " + rootObject.Data.Games.Competitive.Lost);
        Console.WriteLine("Rank: " + rootObject.Data.Competitive.Rank);
    }

    public class Quick
    {
        // Free case support!
        public string Wins { get; set; }
    }

    public class Competitive
    {
        public string Wins { get; set; }  // you may want to check this string here ;)
        public int Lost { get; set; }
        public string Played { get; set; }
    }

    public class Games
    {
        public Quick Quick { get; set; }
        public Competitive Competitive { get; set; }
    }

    public class Playtime
    {
        public string Quick { get; set; }
        public string Competitive { get; set; }
    }

    public class Competitive2
    {
        public string Rank { get; set; }
        // attribute ftw! http://www.newtonsoft.com/json/help/html/SerializationAttributes.htm
        [JsonProperty(PropertyName = "rank_img")]
        public string RankImg { get; set; }
    }

    public class Data
    {
        public string Username { get; set; }
        public int Level { get; set; }
        public Games Games { get; set; }
        public Playtime Playtime { get; set; }
        public string Avatar { get; set; }
        public Competitive2 Competitive { get; set; }
        public string LevelFrame { get; set; }
        public string Star { get; set; }
    }

    public class RootObject
    {
        public Data Data { get; set; }
    }
}

输出

Stats for user Rezoh:
Wins: 82
Losses: 85
Rank: 3392

如果QuickCompetitive是Game,也许:

public abstract class Game
{
    public string Wins { get; set; } // you may want to check this string here ;)
    public int Lost { get; set; }
    public string Played { get; set; }
}

public class Quick : Game // note that Quick game has Lost and PLayed now!
{
}

public class Competitive : Game
{
}

甚至(如@EpicSam在评论中建议的那样):

public class Game
{
    public string Wins { get; set; } // you may want to check this string here ;)
    public int Lost { get; set; }
    public string Played { get; set; }
}

public class Games
{
    public Game Quick { get; set; }
    public Game Competitive { get; set; }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章