将JSON int打印到文本框中

时间:2017-05-27 08:11:20

标签: c# json deserialization

我似乎无法正确映射此json网页的类。

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

类:

Question

我无法将Skin:Graffiti Hunting Rifle的值打印到文本框中,返回的字符串为0,因为我没有正确映射它。

2 个答案:

答案 0 :(得分:0)

我认为你打算在反序列化时使用Rootobject。试试这个;

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

答案 1 :(得分: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