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