-1

I have the following code that populates var data from a webpage using JsonConvert on clicking the Go button, what i need is to still access the data rootobject in a separate loop ie the generatealliancelist at the bottom but im not sure on how to declare data so it is visible from everywhere?

heres the code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public class Rootobject
    {
        public Player[] players { get; set; }
        public Alliance[] alliances { get; set; }
        public Base[] bases { get; set; }
        public Poi[] pois { get; set; }
    }

    public class Player
    {
        public string i { get; set; } //player ID
        public string p { get; set; } 
        public string a { get; set; } //alliance ID
        public string n { get; set; } //player name
        public string f { get; set; } //faction (1 GDI, 2 NOD)
        public string ps { get; set; } 
        public string pd { get; set; }
        public string bc { get; set; }
    }
    public class Alliance
    {
        public string a { get; set; } //alliance ID
        public string an { get; set; } //alliance name
        public string p { get; set; } 
        public string c { get; set; } //player count
    }
    public class Base
    {
        public string pi { get; set; } //player ID
        public string y { get; set; } //coordinates
        public string x { get; set; } //coordinates
        public string n { get; set; } //base name
        public string i { get; set; } //base ID
        public string l { get; set; } //base level
        public string al { get; set; } //is base alerted
        public string pr { get; set; } //has shield
        public string cb { get; set; } //building condition in%
        public string cd { get; set; } //defense condition in %
        public string ps { get; set; } //time in ms before shield drop
    }
    public class Poi
    {
        public string x { get; set; } //coordinates
        public string y { get; set; } //coordinates
        public string t { get; set; } //type from I (Tiberium) to 7(defense) , 0 is tunnel exit 
        public string l { get; set; } //level
        public string a { get; set; } //alliance id owner
    }

    private void button1_Click(object sender, EventArgs e)
    {            
        System.Net.WebClient wc = new System.Net.WebClient();
        string jsonData = wc.DownloadString("http://ccta.hodor.ninja/mapdata/13"); //WORLD4
        Regex regex = new Regex("ccmapData = ({.*}]),\"timestamp\":\"(.*)\",\"world_size\":\"(.*)\"");
        Match match = regex.Match(jsonData);
        System.DateTime timestamp = UnixTimeStampToDateTime(Convert.ToDouble(match.Groups[2].Value));
        var worldsize = match.Groups[3].Value;
        var data = JsonConvert.DeserializeObject<Rootobject>(match.Groups[1].Value + "}");
}

public void generatealliancelist()
    {
        foreach (Alliance alliance in data.alliances) // ERROR the name data does not exist in the current context
        {

        }
    }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Dwayne Dibbley
  • 355
  • 3
  • 20

2 Answers2

2

Instead of making the variable "available anywhere", (which is considered bad design) you should pass it to the GenerateAllianceList method as a parameter:

public void GenerateAllianceList(IEnumerable<T> allicances) // Fill in the correct type instead of `T`
    {
        foreach (Alliance alliance in allicances) 
        {

        }
    }
}

The call the method as follows:

GenerateAllicanceList(data.Alliances);

Also, your property names are atrocious. Give them descriptive names instead of explaining them in comments.

Edit:

OP mentioned the properties are like that because that's how they're called in the Json data. In that case, one should use JsonProperty (or DataMember) attributes to serialize these unfortunate names to readable ones. See: How can I change property names when serializing with Json.net?

Community
  • 1
  • 1
Rik
  • 28,507
  • 14
  • 48
  • 67
  • Thanks for the pointer, works a treat. Also the property names are like that as thats what they are in the JSON data. – Dwayne Dibbley Nov 02 '15 at 15:31
  • That's unfortunate. You should use `JsonProperty` (or `DataMember`) attribute to serialize these unfortunate names to readable ones. See: http://stackoverflow.com/questions/8796618/how-can-i-change-property-names-when-serializing-with-json-net – Rik Nov 02 '15 at 16:38
1

1.You can either make this a field in your class - I think that's a bad idea since we are talking about a local temp var that changes based on a button click.

2.You should pass the var as a method param - Better in this case:

public void generatealliancelist(Rootobject data)
{
        foreach (Alliance alliance in data.alliances) // ERROR the name data does not exist in the current context
        {

        }
    }
}
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99