From this site, I've learned that ASP.NET script services accepting JSON actually require them to be serialized JSON strings (see: "JSON, objects, and strings: oh my!" section of the link). Is there a quick and easy way to serialize them for ASP.NET AJAX consumption on the client side instead of trying to manually convert a bunch of existing objects to JSON-looking strings?
Thanks in advance!
You can use JSON.stringify() to serialize client-side objects for consumption in ASP.NET's Script Services.
Using that approach, you can map client-side objects to server-side objects very easily. ASP.NET will automatically handle converting the JSON to objects (or even collections of objects) for you.
The article writer is confusing Javascript objects with JSON strings. There is no such thing as a "JSON object".
Naturally if you try to send an object to a web service, it has to be serialised, as the request data can only contain text, not objects. The standard way of serialising data to be posted is URL encoding it, so that is what jQuery does.
There is no JSON serialisation built into Javascript or jQuery. You would have to do the serialising yourself or find a library that does it. Here are some options: Serializing to JSON in jQuery
Also, the data sent in the example is not valid JSON. It looks like this:
"{'fname':'dave', 'lname':'ward'}"
To be valid JSON it should look like this:
'{"fname":"dave", "lname":"ward"}'
Related
I'm building a web app, and need to be able to encode user-generated data as concisely as possible before transmitting it to my server. In the past I've used Flash, and it had a very neat system where for any class that you want to serialize, you could write a pair of functions that would describe exactly how to serialize the data. For example:
out.writeShort(session);
out.writeUnsignedInt(itemID);
out.writeObject(arbitraryData);
out.writeShort(score);
You would have to write an equivalent function to read bytes from the serialized data and build the class from it.
Once data is serialized it could be encoded into a Base64 string for safe network transmission to the server.
I can't figure out how to do this in Javascript? JSON is nice and easy but it's incredibly wasteful, sending all object key/value pairs, and unless I'm mistaken everything is encoded as a string? So the value false is encoded as the string "false"?
Any advice on how to implement this in Javascript would be greatly appreciated! Use of libraries is fine so long as they work both on Node and in browser.
Look at this answer. You can use BSON format (Binary JSON) and it doesn't have those features of JSON you mentioned.
While writing node js APIs, we can send plain JSON objects as params (body params), I think there must be some additional overhead for the formatting instead what if I stringify the JSON while sending to API and will parse back to original JSON while processing it.
Can you guys please suggest if this approach gives any performance advantage?
JSON is "JavaScript Object Notation" — i.e. some string format to serialize Object, to save, transfer and restore Object to/via/from a string. JSON.stringify() does not mean you stringify JSON: JSON here is not an object of stringifying, but a namespace like in Math.sqrt(). You cannot transfer Object as is via HTTP or IPC: for this purpose, you have a text serialization format, i.e. JSON.
I have some identifiers on my page. Named id[0], id[1] all the way to id[20] and sometimes more. I need to send these to an MVC controller and I would like to package them up as one object and unpack at the controller. Can someone tell me if this is possible, my knowledge of javascript is just basic so I'd really appreciate advice on which way to go. For example can I use JSON or serialize. btw I'm using jQuery.
Gordon
You can send your data in JSON.
See > ASP.NET MVC How to pass JSON object from View to Controller as Parameter
If you already have the id array (id[0], id1 , ... id[20]) you can convert it to JSON string with respect of JSON.stringify(id) where JSON.stringify function defined in the json2.js.
On the server side you can use for example Deserialize method of the JavaScriptSerializer to convert the data to List<T> where T is type of id[i] (for example string).
How to send data with respect of jQuery.ajax you already know from another answer.
For a data send, where the return data contains potential updates for hundreds of elements on a page, is XML or JSON or another data format better, for usage with jQuery's various parsing formats (invoked via ajax() success)?
Check out this article, it outlines various pros/cons of XML, JSON and HTML when processing AJAX requests.
Personally I'd pick JSON as it uses less bandwidth & is easier to parse and use.
It sounds like a lot of data being returned so json. It's lighter and more compact. Plus it has native use instead of having to parse the xml and traverse it afterwards.
In javascript it is better to go with JSON because it is easier to code and less data to load from the server, unlike XML you have to write a code to parse the elements and fetch the values to your object and for every change in data tags or elements in XML you will need to modify your javascript code which means more coding and testing, unlike JSON all what you need is eval() and you are ready to go.
I need to perform a lot of serializing and de-serializing in an application.
The data is POST parameters and Cookie headers.
Post data example:
Name=Jonathan+Doe&Age=23&Formula=a+%2B+b+%3D%3D+13%25%21
Set-Cookie data example:
ASP.NET_SessionId=esur3bqqgrmnci45um4xegye; path=/
Does anyone have any fast libraries to suggest or efficient ways to perform these operations?
Consider using a JSON stringify method:
http://zumbrunn.com/mochazone/JSON.stringify+and+JSON.parse/
JSON is a fairly universal serialization format that you can parse in ASP.Net. Basically it converts a JS object into a string of text that represents the object itself (as if you were to type the object declaration out literally).