How to create JSON from TEXTAREA based line breaks?

I want to analyze the TEXTAREA, placing each row in the array of JSON via jQuery. Any idea how to do this?

+4
source share
1 answer

Similar:

var array = $('textarea').val().split(/\n|\r/); 

So, you get an array of JavaScript object, not a JSON string. If you want a JSON string, you can use the jquery-the json plugin , and then follow these steps:

 var jsonString = $.toJSON( $('textarea').val().split(/\n|\r/) ); 

And if you want an object, not an array, encoded in the JSON, do the following:

 var o = {}; $( $('textarea').val().split(/\n|\r/) ).each(function(i){ o[i] = this; }); var jsonString = $.toJSON(o); val () split (/ \ n |.. \ R /)) .each (function (i) { var o = {}; $( $('textarea').val().split(/\n|\r/) ).each(function(i){ o[i] = this; }); var jsonString = $.toJSON(o); 
+6
source

All Articles