1

I have a JSON which looks like this...

    {
      "ways": [
        {
          "wayType": "oneway",
          "roadType": "cemented",
          "sources": {
            "mode": "road",
            "resourceName": "ABJ Cement",
            "activeType": {
              "dealerName": "Enterprise CC",
              "dealerAddress": "XYZ"
            },
            "activeCatalog": "CC Catalog Unit 2",
            "activeOwner": "John",
            "activeOwnerPin": "2238889",
            "isVerified": true
          }
        },
        {
          "wayType": "oneway",
          "roadType": "RCC",
          "sources": {
            "mode": "road v2",
            "resourceName": "Global Unit Cement",
            "activeType": {
              "dealerName": "Catch Metal",
              "dealerAddress": "XYZ"
            },
            "activeCatalog": "CC Catalog Unit 2",
            "activeOwner": "John",
            "activeOwnerPin": "2238889",
            "isVerified": true
          }
        }
      ]
    }

I have created a JSON structure Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Console
{ 
    public class activeType
    {
        public string dealerName{ get; set; }
        public string dealerAddress{ get; set; }
    }
    public class sources
    {
        public string mode { get; set; }
        public string resourceName { get; set; }
        public ActiveType activeType { get; set; }
        public string activeCatalog { get; set; }
        public string activeOwner { get; set; }
        public string activeOwnerPin { get; set; }
        public bool isVerified{ get; set; }
    }

    public class ways
    {
        public string wayType{ get; set; }
        public Sources sources { get; set; }
    }
    public class Root
    {
        public List<Step> ways { get; set; }
    }
}

I want to parse it key by key and store it in a variable, I have created a class where I am reading the JSON file from file directory and then opening it. I am creating a console application.

How can I parse the JSON and get the value and store it. Any link to a documentation will be helpful.

SharadxDutta
  • 1,058
  • 8
  • 21
  • 1
    What is a getter class? – ProgrammingLlama Mar 30 '21 at 09:28
  • Don´t re-invent the wheel. There are libs that you can easily that de-serialize json into objects, e.g. NewtonSofts `JsonConvert`. – MakePeaceGreatAgain Mar 30 '21 at 09:30
  • 1
    Carefull you have so mismatch class `public ActiveType activeType { get; set; }` and `activeType`. An in order to deals with uncapitlized properties name I will advice for using Json property descriptor `[JsonProperty("wayType")]` in roder to map `wayType` from json to the capitilized `WayType`. You may also consider simply using tool to creat those class that will save time and do all that. Pasting your json in https://app.quicktype.io. and voila – Self Mar 30 '21 at 09:44

0 Answers0