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

share|improve this question
feedback

2 Answers

up vote 3 down vote accepted

use new JavaScriptSerializer.Deserialize<object>(jsonString)

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

Namespace: System.Web.Script.Serialization

for more info MSDN

share|improve this answer
hey thanks !! is this right ?? JavaScriptSerializer j = new JavaScriptSerializer.Deserialize<object>(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 5: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 5:11
updated my answer.. you dnt need newtonsoft.dll! zzz.. what do you mean return the object?? – Baz1nga Nov 28 '11 at 5: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 5:34
I am using .net framework 3.5 – Parth Doshi Nov 28 '11 at 5:44
show 3 more comments
feedback

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

share|improve this answer
thanks a lot for the help !! :-) – Parth Doshi Jun 18 '12 at 2:50
feedback

Your Answer

 
or
required, but never shown
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.