使用var global可避免在当前上下文中不存在该名称

时间:2015-11-02 13:19:09

标签: c# json class

我有以下代码在单击Go按钮时使用JsonConvert从网页填充var数据,我需要的是仍然在一个单独的循环中访问数据根对象,即底部的generatealliancelist但我不确定如何声明数据,以便从任何地方都可以看到它?

继承人代码:

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
        {

        }
    }
}

2 个答案:

答案 0 :(得分:2)

不要让变量“随处可用”(这被认为是糟糕的设计),而应将其作为参数传递给GenerateAllianceList方法:

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

        }
    }
}

调用方法如下:

GenerateAllicanceList(data.Alliances);

此外,您的房产名称非常糟糕。给他们描述性的名称,而不是在评论中解释。

编辑:

OP提到属性是这样的,因为这是他们在Json数据中调用它们的方式。在这种情况下,应该使用JsonProperty(或DataMember)属性将这些不幸的名称序列化为可读的名称。请参阅:How can I change property names when serializing with Json.net?

答案 1 :(得分:1)

1.你可以把它作为你班上的一个领域 - 我认为这是一个糟糕的想法,因为我们正在讨论一个基于按钮点击而改变的本地临时变量。

2.您应该将var作为方法参数传递 - 在这种情况下更好:

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

        }
    }
}