1

In the following link, a .Net object is passed in an Ajax call using the json2.js javascript library.

http://encosia.com/using-complex-types-to-make-calling-services-less-complex/

Is there a similar method using the Json.Net javascript libary? I can't find anything on it...

I know how to serialize and deserialize objects using this library, but it's all done on the server side.

What if I need to serialize an object on the client side similar to the call in the above link? Anything available using the Json.Net library?

sagesky36
  • 4,542
  • 19
  • 82
  • 130
  • is json.net a javascript library? – Kevin B Jan 06 '14 at 23:12
  • 1
    The way i see it, json.net is just a .net library, not javascript, therefore there is nothing that it can do to help you convert an object to json using javascript. .net runs on the server, not the client. – Kevin B Jan 06 '14 at 23:13
  • What are you trying to do, send data to the server as json or receive on the client as json? – Alex Shilman Jan 06 '14 at 23:16

1 Answers1

1

I think you have some misconceptions here. Json.Net is a third-party JSON library written for .NET in C#. There is no javascript component to it at all. It is commonly used on servers to deserialize requests from, and serialize responses to, clients, which may or may not be javascript-based. The library could also be used in a C#-based client.

The article you referenced demonstrates creating a javascript object and serializing it to JSON using the stringify method from the json2.js library. The resulting serialized string is then sent to a server using jQuery ajax call, and is handled by an ASMX WebMethod.

I have to point out that that article was written in June 2009. Nowadays (nearly 5 years later), most browsers have built-in support for JSON serialization using the very same stringify method syntax shown in the article. So you don't even need a third-party library for that, just call JSON.stringify() directly. For those browsers that don't support it, you can still use json2.js.

Similarly, there are newer, better technologies available on the server side than ASMX, for example ASP.NET Web API, which uses Json.Net by default behind the scenes.

Community
  • 1
  • 1
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • Brian, if I want to serialize a response back to the client via a .Net object (from a C# server method using Json.Net into a string) for use in a jquery grid, this would be the way to go? – sagesky36 Jan 07 '14 at 14:34
  • Yes, I would definitely recommend using Json.Net to do serialization; and I would recommend using Web API (which uses Json.Net) to build a web service. With Web API, you don't have to do anything special to deserialize requests/serialize responses; the framework takes care of it for you. – Brian Rogers Jan 07 '14 at 15:24