viernes, 15 de noviembre de 2013

Convertir un Array de Objetos Javascript a JSon


I made it that way:

if I have:

var jsonArg1 = new Object();
    jsonArg1.name = 'calc this';
    jsonArg1.value = 3.1415;
var jsonArg2 = new Object();
    jsonArg2.name = 'calc this again';
    jsonArg2.value = 2.73;

var pluginArrayArg = new Array();
    pluginArrayArg.push(jsonArg1);
    pluginArrayArg.push(jsonArg2);
to convert pluginArrayArg (which is pure javascript array) into JSON array:

var jsonArray = JSON.parse(JSON.stringify(pluginArrayArg))


Gracias al aporte de:
http://stackoverflow.com/questions/2295496/convert-array-to-json


Como se hace en AJAX:

$.ajax({

                url:"contextPath/data",

                type:"POST",

                contentType: "application/json; charset=utf-8",

                data: JSON.stringify(pluginArrayArg)  , //Stringified Json Object

                async: false,    //Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation

                cache: false,    //This will force requested pages not to be cached by the browser

                processData:false, //To avoid making query String instead of JSON

                success: function(resposeJsonObject){
alert(resposeJsonObject);
        }});

No hay comentarios:

Publicar un comentario