Active2 years, 2 months ago
This question already has an answer here:
JSON-Serialize.js provides conventions and helpers to manage serialization and deserialization of instances to/from JSON - kmalakoff/json-serialize. Serializing and Deserializing JSON The quickest method of converting between JSON text and a.NET object is using the JsonSerializer. The JsonSerializer converts.NET objects into their JSON equivalent and back again by mapping the.NET object property names to the JSON.
No, the standard way to serialize to JSON is to use an existing JSON serialization library. If you don't wish to do this, then you're going to have to write your own serialization methods. If you want guidance on how to do this, I'd suggest examining the source of some of the available libraries.
- Convert form data to JavaScript object with jQuery 48 answers
I want to do some pre-server-validation of a form in a Backbone.js model. To do this I need to get the user input from a form into usable data.I found three methods to do this:
var input = $('#inputId').val();
var input = $('form.login').serialize();
var input = $('form.login').serializeArray();
Unfortunately, none of the provide a good reabable and developable JSON object which I require. I already looked through several questions on Stack Overflow, but I found only some extra libraries.
Doesn't Underscore.js, the current jQuery or Backbone.js provide a helper method?
I can't imagine there is no request for such a function.
HTML
JavaScript
Outputs
Backbone.js model
Peter Mortensen14.5k1919 gold badges8989 silver badges118118 bronze badges
dev.pusdev.pus3,2571111 gold badges3232 silver badges4747 bronze badges
marked as duplicate by Tushar Gupta - curioustushar jqueryOct 15 '14 at 15:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
15 Answers
chim6,64222 gold badges3939 silver badges5252 bronze badges
Maciej PyszyńskiMaciej Pyszyński6,88733 gold badges1818 silver badges2727 bronze badges
You can do this:
see this: http://www.json.org/js.html
vsync56k3939 gold badges180180 silver badges248248 bronze badges
Mohammad AdilMohammad Adil40.4k1515 gold badges7777 silver badges102102 bronze badges
Peter Mortensen14.5k1919 gold badges8989 silver badges118118 bronze badges
STEELSTEEL3,02444 gold badges3737 silver badges6565 bronze badges
Peter Mortensen14.5k1919 gold badges8989 silver badges118118 bronze badges
MaertzMaertz4,59222 gold badges1414 silver badges2525 bronze badges
I know this doesn't meet the helper function requirement, but the way I've done this is using jQuery's $.each() method
Then I can pass loginFormObject to my backend, or you could create a userobject and save() it in backbone as well.
ryandayryanday
I couldn't find an answer that would solve this:
This calls for this object:
So I had to write a serializer of my own that would solve this:
Maybe it will help somebody.
Code Uniquely5,43833 gold badges2525 silver badges3434 bronze badges
user3664916user3664916
If you do not care about repetitive form elements with the same name, then you can do:
I am using Underscore.js here.
Peter Mortensen14.5k1919 gold badges8989 silver badges118118 bronze badges
MitarMitar4,43022 gold badges3838 silver badges5959 bronze badges
Trying to solve the same problem (validation without getting into complex plugins and libraries), I created jQuery.serializeJSON, that improves serializeArray to support any kind of nested objects.
This plugin got very popular, but in another project I was using Backbone.js, where I would like to write the validation logic in the Backbone.js models. Then I created Backbone.Formwell, which allows you to show the errors returned by the validation method directly in the form.
Peter Mortensen14.5k1919 gold badges8989 silver badges118118 bronze badges
tothemariotothemario3,62122 gold badges3232 silver badges3131 bronze badges
If you are sending the form with JSON you must remove [] in the sending string. You can do that with the jQuery function serializeObject():
Peter Mortensen14.5k1919 gold badges8989 silver badges118118 bronze badges
user1990497user1990497
Here is what I use for this situation as a module (in my formhelper.js):
It kind of sucks that I can't seem to find another way to do what I want to do.
This does return this JSON for me:
Peter Mortensen14.5k1919 gold badges8989 silver badges118118 bronze badges
JohnstonJohnston9,51799 gold badges5050 silver badges9494 bronze badges
Peter Mortensen14.5k1919 gold badges8989 silver badges118118 bronze badges
muZkmuZk
Using jQuery and avoiding
serializeArray
, the following code serializes and sends the form data in JSON format:rbarriusorbarriuso
Peter Mortensen14.5k1919 gold badges8989 silver badges118118 bronze badges
19861986
Well, here's a handy plugin for it: https://github.com/macek/jquery-serialize-object
The issue for it is:
Moving ahead, on top of core serialization, .serializeObject will support correct serializaton for boolean and number values, resulting valid types for both cases.
Look forward to these in >= 2.1.0
YaxingYaxing
Found one possible helper:
and for people who don't want to get in contact with forms at all:https://github.com/powmedia/backbone-forms
I will take a closer look at the first link and than give some feedback :)
Javascript Json Serializer
dev.pus
Node Js Serialize Json
dev.pus3,2571111 gold badges3232 silver badges4747 bronze badges