2017-10-19 209 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");