C#中的DataSable对象的JSON字符串

时间:2016-12-19 15:27:12

标签: c# json datatable

基本上我到处都搜索过,而且我找不到任何适用于我情况的东西。

我正在使用Overwatch(一款游戏)的API,我想从网上下载一个String,并检查它是否有一个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,但是大多数人说要在转换之前设置一个类,

问题是:我有2&#34;胜利&#34;:和3&#34;竞争&#34;:正如您在JSON字符串中看到的那样。

因此,在这种情况下,我的信念是不可能的。我尝试制作一个新的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

在我的情况下,没有在线解决方案可以帮助我。

编辑:

这个解决方案对我来说都不起作用:

convert json String to datatable? 或这个 Nested Json String to DataTable

这也不是:

  var token = JToken.Parse(finaldata);

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

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

1 个答案:

答案 0 :(得分:1)

你可以按照他们的说法使用课程。我使用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是游戏,可能是:

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; }
}