2017-05-27 71 views
0

我似乎無法正確映射此json網頁的類。將JSON int打印到文本框中

using (var webClient = new System.Net.WebClient()) 
     { 
      var json = webClient.DownloadString(@"https://opskins.com/api/user_api.php?request=GetLowestSalePrices&key=bac6e59d7edf4be1529adf53a1a5f8&appid=295110&contextid=1&names=Skin:+Graffiti+Hunting+Rifle"); 
      var jsonData = JsonConvert.DeserializeObject<Items>(json); 

      richTextBox1.Text = jsonData.SkinGraffitiHuntingRifle.ToString(); 
     } 
    } 

{"result":{"appid":295110,"contextid":1,"items":{"Skin: Graffiti Hunting Rifle":11990}}} 

類:

public class Rootobject 
{ 
    public Result result { get; set; } 
} 

public class Result 
{ 
    public int appid { get; set; } 
    public int contextid { get; set; } 
    public Items items { get; set; } 
} 

public class Items 
{ 
    public int SkinGraffitiHuntingRifle { get; set; } 
} 
} 

我不能打印出皮膚的價值:塗鴉獵槍到文本框,返回的字符串是0,因爲我沒有正確映射。

回答

0

我想你打算在反序列化時使用Rootobject。嘗試這個 ;

var jsonData = JsonConvert.DeserializeObject<Rootobject>(json); 
0

嗨SkinGraffitiHuntingRifle映射看起來錯了,沒有id的屬性名稱。我們可以爲此做一個解決方法。

using (var webClient = new System.Net.WebClient()) 
     { 
      var json = webClient.DownloadString(@"https://opskins.com/api/user_api.php?request=GetLowestSalePrices&key=bac6e59d7edf4be1529adf53a1a5f8&appid=295110&contextid=1&names=Skin:+Graffiti+Hunting+Rifle"); 
      var jsonData = JsonConvert.DeserializeObject<Rootobject>(json); 
      richTextBox1.Text = jsonData.result.items.SkinGraffitiHuntingRifle.ToString(); 
     } 

類別:

public class Rootobject 
{ 
    public Result result { get; set; } 
} 

public class Result 
{ 
    public int appid { get; set; } 
    public int contextid { get; set; } 
    public Items items { get; set; } 
} 

public class Items 
{ 
    [JsonProperty(PropertyName = "Skin: Graffiti Hunting Rifle")] 
    public int SkinGraffitiHuntingRifle { get; set; } 
} 

JsonPropertyName