使用json.net

时间:2016-09-29 20:56:42

标签: c# json implicit

我自己创建的这门课程:

    public class member
{
    public string account_name { get; set; }
    public long account_id { get; set; }
    public Rootobject[] rootobject { get; set; }
}

这是使用示例JSON答案自动为我创建的类:

public class Rootobject
{
    public string status { get; set; }
    public Meta meta { get; set; }
    public Data data { get; set; }
}
public class Meta
{
    public int count { get; set; }
}
public class Data
{
    public _507888780[] _507888780 { get; set; }
}
public class _507888780
{
    public All all { get; set; }
    public int tank_id { get; set; }
}
public class All
{
    public int spotted { get; set; }
    public int hits_percents { get; set; }
    public int wins { get; set; }
    ...
}

我使用的API服务器的JSON响应的一小部分如下所示:

{
"status": "ok",
"meta": {
    "count": 1
},
"data": {
    "507888780": [
        {
            "all": {
                "spotted": 467,
                "hits_percents": 83,
                "wins": 281,
            },
            "tank_id": 2849
        },
        {
            "all": {
                "spotted": 224,
                "hits_percents": 63,
                "wins": 32,
            },
            "tank_id": 9473
        },
        }

}

这是我用来读出成员拥有的坦克(包括所有统计数据)的代码,其中Request(字符串)只是http请求。

private List<member> memberlist = new List<member>(100);
private void DoStuff()
{
memberlist = JsonConvert.DeserializeObject<List<member>>(result_member);
foreach (var member in memberlist)
        {
            string result_tank = Request("https://api.worldoftanks.eu/wot/tanks/stats/?application_id=" + application_id + "&account_id=" + member.account_id + "&tank_id=" + tanks + "&fields=all.battles%2C+all.wins%2C+all.damage_dealt%2C+all.frags%2C+all.hits_percents%2C+all.piercings%2C+all.shots%2C+all.spotted%2C+all.survived_battles%2C+all.tanking_factor");
            var Rootobject = JsonConvert.DeserializeObject<Rootobject>(result_tank);
            foreach (var tank in _507888780)
            {
                richTextBox1.Text += Rootobject.data._507888780[tank].tank_id + Rootobject.data._507888780[tank].all.spotted.ToString() + "...";
            }
        }
}

现在,我希望能够搜索所有不同的坦克,包括所有成员的统计数据。现在我收到我要打印的行中的错误“Type Tank_Statistics._507888780不能隐式转换为int。”早些时候我alos得到一个错误的IEnumerable丢失,我现在没有... 无论如何..我无法以某种方式使它工作..如果有人能够帮助我这将是非常友好的;)

1 个答案:

答案 0 :(得分:2)

似乎你应该替换这个

 richTextBox1.Text += Rootobject.data._507888780[tank].tank_id + Rootobject.data._507888780[tank].all.spotted.ToString() + "...";

到这个

richTextBox1.Text += tank.tank_id + tank.all.spotted.ToString() + "...";