1

I am using c#.

Now I am looking to create a "JSONHelper" class, which will Serialize and Deserialize the JSON string data. In my current scenario, I am getting below format string from my web-service which returns JSON type.

I have got a method in my C# code which calls a web-service method, which returns the string JSON type data. See below example.

string userDetails = myWebService.GetMember(username, Password, out Result);

so userDetails value is

{"FullName":"Manoj Singh","username":"Manoj","Miles":2220,"TierStatus":"Gold","TierMiles":23230,"MilesExpiry":12223,"ExpiryDate":"31 January 2011","PersonID":232323,"AccessToken":"sfs23232s232","ActiveCardNo":"232323223"}

Now I want to write JSONHelper Class in which there will be methods to Serialize and Deserialize the string type of JSON, and will return the dictionary type of values.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
Manoj Singh
  • 7,569
  • 34
  • 119
  • 198
  • why reinventing the wheels? Newtonsoft.json(http://james.newtonking.com/pages/json-net.aspx) can handle such scenario. – xandy Feb 08 '11 at 10:34
  • You don't even need a third party library. The FCL contains a JSON serilizer/deserializer. http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx. Assuming you still want to create you own - is there a specific question? Or are you just asking 'how do I do it'? – James Gaunt Feb 08 '11 at 10:42
  • See @James I have given above scenario that I what I am getting from my webservice, now I want to pass that string to my JSONDeserializer sot that it will return Dictionary type of object. – Manoj Singh Feb 08 '11 at 10:46
  • I have seen the http://stackoverflow.com/questions/1056121/how-to-create-json-string-in-c, I am looking something like that, however that is only serializing, I am looking to serialize also and will return the dictionary type of object – Manoj Singh Feb 08 '11 at 10:56
  • Ok, so you need to take your string, parse it, construct a dictionary from the result.... but I think you're after more than this? I don't think you'll get anyone posting a full code solution to the problem - especially when the solution exists in the standard System libraries. – James Gaunt Feb 08 '11 at 10:56
  • But the example code is using the FCL library? So you don't want to write your own serializer... sorry I see. Will post an example of deserializing using the same library. – James Gaunt Feb 08 '11 at 10:56

2 Answers2

0

Have you looked at http://json.codeplex.com/ ?

I've used it and find json.net very good. Not sure why you'd want to write your own.

dove
  • 20,469
  • 14
  • 82
  • 108
  • Is there any issue of having small class for the JSON serialization and deserialization, above link I have incorporate other code in my project and I am sure my company will not allow this – Manoj Singh Feb 08 '11 at 10:42
-1

You can use the JavaScriptSerializer class in the System.Web.Script.Serialization namespace:

JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
object result = jsonSerializer.DeserializeObject(jsonString);

Assuming your jsonString represents a object this should return a dictionary. But you will need to check the type carefully as there is no guarantee it won't return an IEnumerable for example if you give it a json list.

Better still you can use a strongly type deserialization:

http://pietschsoft.com/post/2008/02/NET-35-JSON-Serialization-using-the-DataContractJsonSerializer.aspx

If you know the return signature of your web method this is a far better solution.

James Gaunt
  • 14,631
  • 2
  • 39
  • 57
  • I am not getting the System.Web.Script.Serialization in my namespace, please note I am using .NET2.0, what did you mean by "If you know the return signature of your web method this is a far better solution." – Manoj Singh Feb 08 '11 at 11:06
  • You need to add the System.Web.Extensions assembly from .NET 3.5 to your solution and get this functionality. .NET 2.0 and 3.5 run on the same underlying virtual machine (i.e. the compiler is the same). This should be possible. Alternatively if you can't do this then you'll need a 3rd party library. By 'knowing the return signature' I meant you know the method always returns a json object of a specific format. But I think DataContractJsonSerializer is ruled out if your stuck with just 2.0. – James Gaunt Feb 08 '11 at 11:39