3

I want to read a JSON file and map it to class objects. How would I do that in C#?

JSON

{  
   "companyName":"Test company",
   "companyNumber":"1234",
   "address":{  
      "buildingNumber":"33",
      "street":"Caledon Road",
      "county":"Barking and Dagenham",
      "postalTown":"Essex",
      "postcode":"E62HE"
   }
}

C# Code

public class CompanyInfo
{ 
    public string companyName{ get;set;}
    public string companyNumber{ get;set;}
    public string buildingNumber{ get;set;}
    public string street{ get;set;}
    public string county{ get;set;}
    public string postalTown{ get;set;}
    public string postCode{ get;set;}
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Vinoth
  • 121
  • 1
  • 1
  • 9

4 Answers4

6

Make your code

    var json = {  
                 "companyName":"Test company",
                 "companyNumber":"1234",
                 "address":{  
                     "buildingNumber":"33",
                     "street":"Caledon Road",
                     "county":"Barking and Dagenham",
                     "postalTown":"Essex",
                     "postcode":"E62HE"
                  }
               }

 public class CompanyInfo
    { 
    public string companyName{ get;set;}
    public string companyNumber{ get;set;}
    public Address address {get;set;}
    }

 public class Address
   {
    public string buildingNumber{ get;set;}
    public string street{ get;set;}
    public string county{ get;set;}
    public string postalTown{ get;set;}
    public string postCode{ get;set;}
   }

then use Newtonsoft.Json to Deserialize the json

var results = JsonConvert.DeserializeObject<CompanyInfo>(json);
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
5

First create a class to match the JSON, this can be easily done using this super-handy tool json2csharp.com

the JSON you provided converts to this

public class Address
    {
        public string buildingNumber { get; set; }
        public string street { get; set; }
        public string county { get; set; }
        public string postalTown { get; set; }
        public string postcode { get; set; }
    }

    public class RootObject
    {
        public string companyName { get; set; }
        public string companyNumber { get; set; }
        public Address address { get; set; }
    }

Then deserialize your JSON into an object of the type you just defined using JSON.net (nuget Install-Package Newtonsoft.Json)

public void LoadJson()
{
    using (StreamReader r = new StreamReader("file.json"))
    {
        string json = r.ReadToEnd();
        RootObject company = JsonConvert.DeserializeObject<RootObject>(json);
   }
}

I suggest renaming RootObject to something more meaningful in your application

2

You need two classes - CompanyInfo and Address. CompanyInfo must contain Address object, because json have address object in companyInfo:

public class CompanyInfo
{ 
    public string companyName{ get;set;}
    public string companyNumber{ get;set;}
    public Address address{get;set;}
}

public class Address
{
    public string buildingNumber{ get;set;}
    public string street{ get;set;}
    public string county{ get;set;}
    public string postalTown{ get;set;}
    public string postCode{ get;set;}
}

Then you should deserialize json using Newtonsoft.Json NuGet Package or something other.

Roman
  • 11,966
  • 10
  • 38
  • 47
0

You have to use JSON.Net enter link description here

This article can be interesting too: How can I parse JSON with C#?

Community
  • 1
  • 1
Benoît
  • 87
  • 8