简体   繁体   English

使用json.net从C#中的反序列化对象类中获取数据

[英]Getting data out of deserialized object classes in C# using json.net

This class I created myself: 我创建了自己的这堂课:

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

This are the classes VS created for me autmatically using an example JSON answer: 这是VS使用示例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; }
    ...
}

A small part of the JSON response from the API server I use looks like this: 我使用的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
        },
        }

} }

This is the code I use to read out the tanks a member has (including all the stats) where Request(string) is just the http request. 这是我用来读取成员拥有的储箱(包括所有统计信息)的代码,其中Request(string)只是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() + "...";
            }
        }
}

Now, I want to be able to search up all the different tanks including their stats for all members. 现在,我希望能够搜索所有不同的战车,包括所有成员的状态。 Right now I'm getting the error in the line I want to print "Type Tank_Statistics._507888780 cannot be implicitly converted to int." 现在,我在要打印的行中遇到错误“类型Tank_Statistics._507888780无法隐式转换为int。” Earlier on I alos got an error with a missing IEnumerable which I dont have right now though.. Anyways .. I can't make it work somehow.. it would be very kind if someone would be able to help me on this ;) 早些时候,我Alos遇到了一个丢失的IEnumerable的错误,尽管我现在没有。.无论如何..我无法使其工作..如果有人能够在这个问题上为我提供帮助,那将非常好;)

Seems that you should replace this 似乎您应该更换它

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

to this 对此

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM