1 Reply Latest reply on Apr 24, 2015 2:11 AM by nilsheuer

    Deserializing JSON with C# Tutorial

      Deserializing JSON with C# Tutorial

      Code Trench

      Json.NET is a library that makes serializing and deserializing JSON with C# really easy. In this article we’ll create a scenario where we need to deserialize a chunk of JSON into an Account object.   First let download the Json.NET package and add it to our project using NuGet:   Lets expect...

      Json.NET is a library that makes serializing and deserializing JSON with C# really easy.

      In this article we’ll create a scenario where we need to deserialize a chunk of JSON into an Account object.

       

      First let download the Json.NET package and add it to our project using NuGet:

        
      1
      Install-Package Newtonsoft.Json

       

      Lets expect to receive the following account JSON of from a web service:

        
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      {
          "id": 123,
          "firstname": "Doug",
          "lastname": "Palmer",
          "address": "42 HillCrest Ave.",
          "city": "Vancouver",
          "postalcode": "V7R1M9",
          "phone1": "6049889345",
          "email": "doug@ex1m3p8le.com"
      }

       

      Next lets create a C# class to represent the Account. Each property maps to a field in the JSON above:

        
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      using Newtonsoft.Json;
      using System;
      using System.Collections.ObjectModel;
       
      namespace Example
      {
          public class Account
          {
              [JsonProperty(PropertyName = "id")]
              public int Id { get; set; }
       
              [JsonProperty(PropertyName = "firstname")]
              public string Firstname { get; set; }
       
              [JsonProperty(PropertyName = "lastname")]
              public string Lastname { get; set; }
       
              [JsonProperty(PropertyName = "address")]
              public string Address { get; set; }
       
              [JsonProperty(PropertyName = "city")]
              public string City { get; set; }
       
              [JsonProperty(PropertyName = "postalcode")]
              public string Postalcode { get; set; }
       
              [JsonProperty(PropertyName = "phone1")]
              public string Phone1 { get; set; }
       
              [JsonProperty(PropertyName = "email")]
              public string Email { get; set; }
       
              public Account()
              {
                  // Setting default values here
                  // With C# 6 we could use auto property initializers above instead!
                  Id         = -1;
                  Firstname  = "";
                  Lastname   = "";
                  Address    = "";
                  City       = "";
                  Postalcode = "";
                  Phone1     = "";
                  Email      = "";
              }
          }
      }

       

      Pro Tip: Reddit user Hurry_Hurry pointed out that you can Paste Special the JSON and it will generate auto-properties for you. You still need to add the Json.NET attributes and default values, but it definitely saves some time.

      screen1      screen2

       

      Now that we have a modal for our accounts we can deserialize the chunk of JSON into an Account object. Doing this is easy with the JsonConvert class. Just pass it the string of JSON and it will return an Account object:

        
      1
      2
      3
      4
      public Account DeserializeAccount(string json)
      {
          return JsonConvert.DeserializeObject<Account>(json);
      }

       

      If you’re receiving a JSON array of accounts, you can even get it to return a List:

        
      1
      2
      3
      4
      public List<Account> DeserializeAccounts(string json)
      {
          return JsonConvert.DeserializeObject<List<Account>>(json);
      }

       

      You can view the official documentation here.

      An interesting article for those of the .NET persuasion. =)  Nils Heuer

      Shared with Jive Anywhere