Read and parse a Json File in C#

The Solution to Read and parse a Json File in C# is

How about making everything easier with Json.NET?

    public void LoadJson()
        using (StreamReader r = new StreamReader("file.json"))
            string json = r.ReadToEnd();
            List<Item> items = JsonConvert.DeserializeObject<List<Item>>(json);

    public class Item
        public int millis;
        public string stamp;
        public DateTime datetime;
        public string light;
        public float temp;
        public float vcc;

You can even get the values dynamically without declaring Item class.

    dynamic array = JsonConvert.DeserializeObject(json);
    foreach(var item in array)
        Console.WriteLine("{0} {1}", item.temp, item.vcc);

~ Answered on 2012-11-08 21:18:01

Most Viewed Questions: