Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何将反序列化的JSON导入公开?_C#_List - Fatal编程技术网

C#如何将反序列化的JSON导入公开?

C#如何将反序列化的JSON导入公开?,c#,list,C#,List,我已经反序列化了一个JSON文件,我无法访问using语句之外的列表myImport。在using语句中,一切正常。如何在外部访问该列表 public class ImportJSON { static public void ImportMyFile() { string myFilePath = @"C:\adf\af\afd\af\afs\CoordData.json"; using (StreamReader sr = new Strea

我已经反序列化了一个JSON文件,我无法访问using语句之外的列表myImport。在using语句中,一切正常。如何在外部访问该列表

public class ImportJSON
{
    static public void ImportMyFile()
    {
        string myFilePath = @"C:\adf\af\afd\af\afs\CoordData.json";

        using (StreamReader sr = new StreamReader(myFilePath))
        {
            string json = sr.ReadToEnd();
            List<Coord> myImport = JsonConvert.DeserializeObject<List<Coord>>(json);
        }

        foreach (var item in myImport)
        {
            Console.WriteLine("ID : " + item.iD);
        }
    }
}
公共类ImportJSON
{
静态公共void ImportMyFile()
{
字符串myFilePath=@“C:\adf\af\afd\af\afs\CoordData.json”;
使用(StreamReader sr=新的StreamReader(myFilePath))
{
字符串json=sr.ReadToEnd();
List myImport=JsonConvert.DeserializeObject(json);
}
foreach(myImport中的var项)
{
Console.WriteLine(“ID:+item.ID”);
}
}
}

在using语句之外声明列表

List<Coord> myImport = new List<Coord>();
using (StreamReader sr = new StreamReader(myFilePath))
{
    string json = sr.ReadToEnd();
    myImport = JsonConvert.DeserializeObject<List<Coord>>(json);
}

foreach (var item in myImport)
{
    Console.WriteLine("ID : " + item.iD);
}
List myImport=new List();
使用(StreamReader sr=新的StreamReader(myFilePath))
{
字符串json=sr.ReadToEnd();
myImport=JsonConvert.DeserializeObject(json);
}
foreach(myImport中的var项)
{
Console.WriteLine(“ID:+item.ID”);
}

使用之前声明它
完美!Thansk for support:)愚蠢的问题,但如何使其在方法之外也可用?如果在方法外部声明列表,则在using语句“myImport在当前上下文中不存在…使其成为静态的”
static list myImport=new list()