2017-10-19 208 views
-2

我已经尝试了无数的方法来解析我的JSON字符串(蒸汽公共数据),但似乎没有任何工作。我只是想能够从字符串中提取值。例如,获得将返回SlothGod的值personaname。我的项目中安装了JSON.NET。C#解析JSON字符串

这里是我的JSON:

{ 
    "response": { 
     "players": [ 
      { 
       "steamid": "76561198301407459", 
       "communityvisibilitystate": 3, 
       "profilestate": 1, 
       "personaname": "SlothGod", 
       "lastlogoff": 1508389707, 
       "commentpermission": 1, 
       "profileurl": "http://steamcommunity.com/id/sleuthgud/", 
       "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/09/09cea52b91136fb3306c57771a746db2823b91ba.jpg", 
       "avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/09/09cea52b91136fb3306c57771a746db2823b91ba_medium.jpg", 
       "avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/09/09cea52b91136fb3306c57771a746db2823b91ba_full.jpg", 
       "personastate": 0, 
       "realname": "Josh", 
       "primaryclanid": "103582791460168790", 
       "timecreated": 1462086929, 
       "personastateflags": 0, 
       "loccountrycode": "AU", 
       "locstatecode": "QLD" 
      } 
     ] 

    } 
} 

主要方法向我建议:

public class Details 
{ 
    public string personaname { get; set; } 
} 
private void GetSteamDetails() 
{ 
    var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Details>(SteamDetailsJson); 
    SteamName = data.personaname; 
} 

这是摆的Page_Load()之前。然后我调用GetSteamDetails();当我想要取名时。

+0

你应该告诉你已经尝试的..我们可以尝试,并帮助解决问题的 –

+3

可能重复[我怎样才能解析JSON的C#?(https://stackoverflow.com/questions/6620165/如何我可以解析JSON与C) – 2017-10-19 06:34:05

+0

@TAHASULTANTEMURI我已经看过那个确切的职位,但我从来没有能够得到它的工作 – SlothGod

回答

0

我的问题是降低投,我决定不放弃对这个问题之后。经过广泛的研究,试验和错误,以及最有用的国际海事组织的YouTube教程。我发现数据包含一个JSON数组,并且是的,我承认,我对此感到困惑,但答案是简单地将它看作一个C#数组,并将[1]添加到播放器的末尾。

Details details = new Details(); 
public class Details 
{ 
    public string avatar { get; set; } 
    public string avatarmedium { get; set; } 
    public string avatarfull { get; set; } 
    public string realname { get; set; } 
    public string personaname { get; set; } 
    public string steamid { get; set; } 
} 

private void GetSteamDetails() 
{ 
    var SteamDetails= JsonConvert.DeserializeObject<dynamic>(SteamDetailsJson); 
    avatar = SteamDetails.response.players[1].avatar.ToString(); 
    personaname = SteamDetails.response.players[1].personaname.ToString(); 
} 
0

您提到Newtonsoft.Json已在项目中引用。

使用类来表示json数据结构,然后您可以轻松地反序列化它。
您只能在班级中使用您需要的属性。

public class Player 
{ 
    public string personaname { get; set; } 
} 

var player = Newtonsoft.Json.JsonConvert.DeserializeObject<Player>(jsonString); 

// use player.personaname 

对于更新问题制作这代表你的数据结构

public class Team 
{ 
    public List<Player> players { get; set; } 
} 

public class Response 
{ 
    public Team response { get; set; } 
} 
+0

我对DeserializeObject方法一直非常困惑,你能解释我添加什么来代替'Data'吗? – SlothGod

+0

创建“数据”类或将其命名为您想要的名称。 – Fabio

+0

我编辑了我的问题并添加了我所做的更改。 SteamName仍然返回null。 – SlothGod

0

您可以使用http://json2csharp.com/从JSON字符串自动生成一个类的类。

+0

我不知道为什么人们下来投这个答案?这个不错。 – 2017-10-19 07:05:03

+0

我不是downvoter,但我想因为“链接”的答案不是很受欢迎SO – Fabio

+0

@Fabio我怀疑这就是为什么。该页面出现故障,此答案无用。除了现有的答案之外,它更好。 – john

0

根据您提供的JSON字符串,您应该有以下C#类来支持它,或者将JSON对象值反序列化为:我使用this链接来生成类。

public class Player 
{ 
    public string steamid { get; set; } 
    public int communityvisibilitystate { get; set; } 
    public int profilestate { get; set; } 
    public string personaname { get; set; } 
    public int lastlogoff { get; set; } 
    public int commentpermission { get; set; } 
    public string profileurl { get; set; } 
    public string avatar { get; set; } 
    public string avatarmedium { get; set; } 
    public string avatarfull { get; set; } 
    public int personastate { get; set; } 
    public string realname { get; set; } 
    public string primaryclanid { get; set; } 
    public int timecreated { get; set; } 
    public int personastateflags { get; set; } 
    public string loccountrycode { get; set; } 
    public string locstatecode { get; set; } 
} 

public class Response 
{ 
    public List<Player> players { get; set; } 
} 

public class RootObject 
{ 
    public Response response { get; set; } 
} 

然后,使用Newtonsoft.Json,你可以反序列化JSON对象到你的C#类如下:

JsonConvert.DeserializeObject<RootObject>("yourJsonString");