bhavanisankarnimmala

Read JSON Object

Blog Post created by bhavanisankarnimmala on May 25, 2018

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;

namespace JsonObject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StreamReader r = new StreamReader(@"D:\DesktopDump\JsonString.txt");
            string json = r.ReadToEnd();
            List<RoadNames> plist = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RoadNames>>(json);
            foreach (RoadNames road in plist)
            {
                string roadName = road._id.ToString();
            }
        }
        public class RoadNames
        {
            public string _id { get; set; }
        }
        public class RoadJunctionNames
        {
            public string junction_coords { get; set; }
            public string junction_name { get; set; }
            public string main_road_name { get; set; }
            public string inter_road_name { get; set; }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            StreamReader r = new StreamReader(@"D:\DesktopDump\JsonString2.txt");
            string json = r.ReadToEnd();
            List<RoadJunctionNames> plist = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RoadJunctionNames>>(json);
            foreach (RoadJunctionNames road in plist)
            {
                string mainRoadName = road.main_road_name.ToString();
                string junctionRoadName = road.junction_name.ToString();
                string interRoadName = road.inter_road_name.ToString();
                string junctionCoordinates = road.junction_coords.ToString();
            }
        }
    }
}

Outcomes