How can I parse JSON with C# ?

How can I parse JSON with C# ?

Asked on November 13, 2018 in C#.
Add Comment


  • 5 Answer(s)

        Here is a simple JSON string. In many cases, you will get this string from a web service call.
    We can also able to do this manually,

    Example:

    var example = @"{""name"":""Venkat"",""age"":70}";
    

    This example is a simple JSON object with 2 fields: name and age.

    Answered on November 13, 2018.
    Add Comment

        If we want the access the fields in the JSON string.
    We need to DESERIALIZE it which should c# understand.

    Below there is an example:

    var JSONObj = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(example1);
    

    This line helps to deserialize the string example into an object of type

    Dictionary<string ,string>
    

    Once we have done that, we can access the fields like this:

    JSONObj["name"]; // equals venkat
    JSONObj["age"]; // equals 30
    

    REMEMBER!!

    First, create a class which matches the definition of your JSON. In that case, we need a class with a string property and an int property:

    class ExampleModel
    {
    public string name { get; set; }
    public int age { get; set; }
    }
    

    And now, to deserialize our JSON into an object of that type:

    And to reference the fields:

    exampleModel.name; // venkat
    exampleModel.age; // equals 30

    Answered on November 13, 2018.
    Add Comment

    The following code helps to parse with JSON:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("URL");
    JArray array = new JArray();
    using (var twitpicResponse = (HttpWebResponse)request.GetResponse())
        {
        using (var reader = new StreamReader(twitpicResponse.GetResponseStream()))
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            var objText = reader.ReadToEnd();
            JObject joResponse = JObject.Parse(objText);
            JObject result = (JObject)joResponse["result"];
            array = (JArray)result["Detail"];
            string statu = array[0]["dlrStat"].ToString();
        }
    }
    
    Answered on November 13, 2018.
    Add Comment

    Here are some options without using third party libraries:

    // For that you will need to add reference to System.Runtime.Serialization
    var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(@"{ ""Name"": ""Jon Smith"", ""Address"": { ""City"": ""New York"", ""State"": ""NY"" }, ""Age"": 42 }"), new System.Xml.XmlDictionaryReaderQuotas());
    
    // For that you will need to add reference to System.Xml and System.Xml.Linq
    var root = XElement.Load(jsonReader);
    Console.WriteLine(root.XPathSelectElement("//Name").Value);
    Console.WriteLine(root.XPathSelectElement("//Address/State").Value);
    
    // For that you will need to add reference to System.Web.Helpers
    dynamic json = System.Web.Helpers.Json.Decode(@"{ ""Name"": ""Jon Smith"", ""Address"": { ""City"": ""New York"", ""State"": ""NY"" }, ""Age"": 42 }");
    Console.WriteLine(json.Name);
    Console.WriteLine(json.Address.State);
    Answered on January 16, 2019.
    Add Comment

    Here is a snippet from that site:

    WebClient webClient = new WebClient();
    dynamic result = JsonValue.Parse(webClient.DownloadString("https://api.foursquare.com/v2/users/self?oauth_token=XXXXXXX"));
    Console.WriteLine(result.response.user.firstName);
    Answered on January 16, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.