问题描述:

I have a JSON file and I want to store his data, print it in a console. I'm trying doing this:

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);

}

How can I read all the people? How can I write all the results in console?

网友答案:
    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);
        }
    }
相关阅读:
Top