none
How to read JSON in C#? RRS feed

  • Question

  • {
    "billingAccountNumber": "449239621",
    "serviceAddress": 
    {
     "address1": "9337 MIRA VALLE LN",
     "stateCode": "FL", 
     "city": "WINTER GARDEN",
     "zip": "34787-",
     "countryCode": "USA",
     "address2": ""
    },
    }

    I have JSON structure, and I need to read json Zip value (e.g 34787 ) in c#.

    how to do this? thanks in advance.

    Tuesday, June 26, 2018 4:17 AM

Answers

  • Hi sushilbhat,

    We could also use Newtonsoft.Json to achieve it. right click your project -> Manage Nuget Package... -> select Newtonsoft.Json -> install, and the following code for your reference.

    Please  add the following using in your class.

    using Newtonsoft.Json.Linq;

    var stringFullOfJson =
                   @"{
                      ""billingAccountNumber"": ""449239621"",
                      ""serviceAddress"":
                      {
                         ""address1"": ""9337 MIRA VALLE LN"",
                         ""zip"": ""34787-""
                      }
                   }";
                JToken token = JObject.Parse(stringFullOfJson);
    
                string zip = (string)token.SelectToken("serviceAddress").SelectToken("zip");

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, June 26, 2018 7:02 AM
    Moderator

All replies

  • Make sure that Target Framework of your project in Project Properties is 4.5 or newer, add references to System.Web.Extensions and Microsoft.CSharp assemblies, and check this example:

       var text =

           @"{

              ""billingAccountNumber"": ""449239621"",

              ""serviceAddress"":

              {

                 ""address1"": ""9337 MIRA VALLE LN"",

                 ""zip"": ""34787-""

              }

           }";

     

       var s = new JavaScriptSerializer();

       var o = s.DeserializeObject( text );

       dynamic d = (IDictionary<string, object>)o;

     

       var zip = d["serviceAddress"]["zip"];

     

       Console.WriteLine( zip );

     

    It is also possible to deserialise to a specific class, or to use some third-party libraries.

     


    Tuesday, June 26, 2018 4:59 AM
  • Hi sushilbhat,

    We could also use Newtonsoft.Json to achieve it. right click your project -> Manage Nuget Package... -> select Newtonsoft.Json -> install, and the following code for your reference.

    Please  add the following using in your class.

    using Newtonsoft.Json.Linq;

    var stringFullOfJson =
                   @"{
                      ""billingAccountNumber"": ""449239621"",
                      ""serviceAddress"":
                      {
                         ""address1"": ""9337 MIRA VALLE LN"",
                         ""zip"": ""34787-""
                      }
                   }";
                JToken token = JObject.Parse(stringFullOfJson);
    
                string zip = (string)token.SelectToken("serviceAddress").SelectToken("zip");

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, June 26, 2018 7:02 AM
    Moderator