28

I have a JSON String returned by my SOAP web service in .NET. It is as follows:

{
 "checkrecord":
   [
     {
      "rollno":"abc2",
      "percentage":40,
      "attended":12,
      "missed":34
     }
  ],
 "Table1":[]
}

Now I want to parse this string to a JSON Object. I also read this where they have used this line of code:

JObject jsonObj = JObject.Parse(json);

So can I do the same by replacing "json" with my string name. Also do I need to reference any other dll except the NewtonSoft.dll ?

BTW, Here is the full webservice code

Albert Lazaro de Lara
  • 2,540
  • 6
  • 26
  • 41
Parth Doshi
  • 4,200
  • 15
  • 79
  • 129

4 Answers4

37

use new JavaScriptSerializer().Deserialize<object>(jsonString)

You need System.Web.Extensions dll and import the following namespace.

Namespace: System.Web.Script.Serialization

for more info MSDN

Eric
  • 4,201
  • 5
  • 27
  • 36
Baz1nga
  • 15,485
  • 3
  • 35
  • 61
  • hey thanks !! is this right ?? JavaScriptSerializer j = new JavaScriptSerializer.Deserialize(json); Also , when I write this line of code in vs 2008, I m getting error saying " The type or namespace JavaScriptSerializer could not be found" I have added Newtonsoft.dll as reference, do I need to add something else – Parth Doshi Nov 28 '11 at 05:09
  • also how do I return the Object? Do I need to change data type of my webmethod to JSON Object? – Parth Doshi Nov 28 '11 at 05:11
  • updated my answer.. you dnt need newtonsoft.dll! zzz.. what do you mean return the object?? – Baz1nga Nov 28 '11 at 05:29
  • I want to know when I convert the JSON String I have into a JSON Object using the method u have mentioned, what will be the format of the JSON object and how will it look like? Also, I need to return that JSON object so that it can be used on my client Android app – Parth Doshi Nov 28 '11 at 05:34
  • well you cannot send a c# object to the client you need to serialize it again and end it and deserialize it in the client side code. – Baz1nga Nov 28 '11 at 05:44
  • http://atsung.wordpress.com/2008/08/07/javascriptserializer-example/ shows how to work with this class.. Please google you will find everything – Baz1nga Nov 28 '11 at 05:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5392/discussion-between-parth-90-and-baz1nga) – Parth Doshi Nov 28 '11 at 05:46
  • 2
    @capdragon: Not unless he's your father. – B. Clay Shannon-B. Crow Raven Oct 15 '13 at 20:28
  • @Baz1nga: I added the System.Web.Extensions reference and then "using System.Web.Script.Serialization;" but this line: var bla = new JavaScriptSerializer.Deserialize(jsonString); ...failed with the dreaded red "who goes there?" on Deserialize. IOW, this: var bla = new JavaScriptSerializer. prompts nothing at all from Intellisense; though it seems to know who JavaScriptSerializer is, it doesn't recognize it as having any available methods. – B. Clay Shannon-B. Crow Raven Oct 15 '13 at 20:36
  • you need to instantiate an object of type JavaScriptSerializer as follows new JavaScriptSerializer().. else I need to see more of your code to help – Baz1nga Oct 16 '13 at 10:04
  • How can one pass in the ? – Demodave Jan 26 '15 at 20:13
  • @Demodave, instantiate the serializer on one line, then call the generic function on the next line. I had the same problem using the syntax in the answer. – adam0101 Jun 09 '15 at 14:49
15

I see that this question is very old, but this is the solution I used for the same problem, and it seems to require a bit less code than the others.

As @Maloric mentioned in his answer to this question:

var jo = JObject.Parse(myJsonString);

To use JObject, you need the following in your class file

using Newtonsoft.Json.Linq;
Community
  • 1
  • 1
Kappacake
  • 1,826
  • 19
  • 38
6

Another choice besides JObject is System.Json.JsonValue for Weak-Typed JSON object.

It also has a JsonValue blob = JsonValue.Parse(json); you can use. The blob will most likely be of type JsonObject which is derived from JsonValue, but could be JsonArray. Check the blob.JsonType if you need to know.

And to answer you question, YES, you may replace json with the name of your actual variable that holds the JSON string. ;-D

There is a System.Json.dll you should add to your project References.

-Jesse

Jesse Chisholm
  • 3,857
  • 1
  • 35
  • 29
  • Just to clarify for anyone trying to locate this .dll on their system, it looks like this is Silverlight dll available with .Net 4.5. – longda Aug 22 '13 at 19:00
3

Since you mentioned that you are using Newtonsoft.dll you can convert a JSON string to an object by using its facilities:

MyClass myClass = JsonConvert.DeserializeObject<MyClass>(your_json_string);

[Serializable]
public class MyClass
{
    public string myVar {get; set;}
    etc.
}
azakgaim
  • 309
  • 3
  • 6