32

Is there any easy way to parse below JSOn in c#

{"type":"text","totalprice":"0.0045","totalgsm":"1","remaincredit":"44.92293","messages": [
{"status":"1","messageid":"234011120530636881","gsm":"923122699633"}
]}

and in case Multiple results.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
user788592
  • 477
  • 1
  • 5
  • 12
  • 4
    use a library like Json.NET – Jehof Dec 02 '15 at 13:00
  • Here's a way to do it [Parsing nested json objects with json.net](http://stackoverflow.com/questions/13204663/parsing-nested-json-objects-with-json-net). – Suprabhat Biswal Dec 02 '15 at 13:02
  • [This](http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) will probably help you while using Json.NET – croxy Dec 02 '15 at 13:03
  • u can refer to this link similar kind of question http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c – Ravi Kanth Dec 02 '15 at 13:08
  • Possible duplicate of [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – Liam Jul 19 '17 at 14:36

4 Answers4

61

Follow these steps:

  1. Convert your JSON to C# using json2csharp.com;
  2. Create a class file and put the above generated code in there;
  3. Add the Newtonsoft.Json library to your project using the Nuget Package Manager;
  4. Convert the JSON received from your service using this code:

     RootObject r = JsonConvert.DeserializeObject<RootObject>(json);
    

(Feel free to rename RootObject to something more meaningful to you. The other classes should remain unchanged.)

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
25

You can safely use built-in JavaScriptSerializer without referencing additional third party libraries:

var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
ser.DeserializeObject(json);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Łukasz Trzewik
  • 1,165
  • 2
  • 11
  • 26
  • 3
    This was helpful to me. DeserializeObject returned an object of type Dictionary. The objects in the dictionary were strings, object arrays, or more objects of Dictionary – John Gilmer Feb 07 '17 at 08:14
  • 4
    IMO this is the way to go if you are just doing a simple parse or if you don't want to use a third party library at all. – YYamil Feb 27 '17 at 18:31
7

I found a way to get it without using any external API

        using (var w = new WebClient())
        {
            var json_data = string.Empty;
            string url = "YOUR URL";
            // attempt to download JSON data as a string
            try
            {
                json_data = w.DownloadString(url);
                JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                var result = jsSerializer.DeserializeObject(json_data);
                Dictionary<string, object> obj2 = new Dictionary<string, object>();
                obj2=(Dictionary<string,object>)(result);

                string val=obj2["KEYNAME"].ToString();
            }
            catch (Exception) { }
            // if string with JSON data is not empty, deserialize it to class and return its instance 
        }
Ashish
  • 83
  • 1
  • 4
1

For me ... the easiest way to do that is using JSON.net do a deserialize to a entity that represents the object, for example:

public class Message
{
    public string status { get; set; }
    public string messageid { get; set; }
    public string gsm { get; set; }
}

public class YourRootEntity
{
    public string type { get; set; }
    public string totalprice { get; set; }
    public string totalgsm { get; set; }
    public string remaincredit { get; set; }
    public List<Message> messages { get; set; }
}

And do this:

YourRootEntity data JsonConvert.DeserializeObject<YourRootEntity>(jsonStrong);
SergioZgz
  • 148
  • 5
  • This won't work. `Message` property have json object, not an array of object. `public List messages { get; set; }` isn't required simple `public Message messages { get; set; }` would work. – Suprabhat Biswal Dec 02 '15 at 13:18
  • I just create it with json2csharp, to write a sample. – SergioZgz Dec 02 '15 at 13:19