如何阅读JSON并写入Console

时间:2015-09-16 15:29:41

标签: c# json

我有一个JSON文件,我想存储他的数据,将其打印在控制台中。我正在尝试这样做:

public class Person
{
    public string name { get; set;}
    public string age { get; set;}
    public string address { get; set;}
}

public void readJson() 
{
    string json = File.ReadAllText(@"C:\json\response.json");
    Person p = Newtonsoft.Json.JsonConvert.DeserializeObject<Person>(json);
}

我怎样才能读到所有人?如何在控制台中写出所有结果?

1 个答案:

答案 0 :(得分:0)

    public class Person
    {

        public string name { get; set;}
        public string age { get; set;}
        public string address { get; set;}
    }

    public void readJson() 
    {
        try
        {
            string json = File.ReadAllText(@"C:\json\response.json");
        }
        catch(Exception ex)
        {
        //TODO:Log or something
        }
        IEnumerable<Person> pList = new List<Person>(); 

        try
        {
        pList =Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Person>>(json);
        }
        catch(Exception ex)
        {
            //TODO:Log or something
        }

        forach(var person in pList)
        {
            Console.ReadLine(person);
        }
    }