简体   繁体   中英

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:

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:

{
"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.

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." 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 ;)

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() + "...";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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