How to build a JSON string from a JSON object

This question already has an answer here:

  • Serializing to JSON in jQuery [duplicate] 11 answers

  • No need for jQuery here:

    var execs = JSON.stringify( window.ob.executives );
    

    Edit

    After OP specifying the structure of the variable, I suggest the following (Traversing through two levels of nested objects, extracting the data to add it to an intermediate object, which can then be serialized):

    var obj = {};
    $.each(window.ob.executives, function( key, val ) {
      $.each( val, function( iKey, iVal ) {
        obj[ iVal ] = iKey;
      });
    });
    var execs = JSON.stringify( obj );
    

    您可以使用JSON.stringify(JSON对象)函数, 它将JSON对象转换为JSON字符串。


    Use this code JSON.stringify(data);

    For eg:

       $.ajax({
                        type: "POST",
                        url: "/Item/Create",
                        data: JSON.stringify({ "item": item, "status": status }),
                        dataType: 'json',
                        contentType: 'application/json;',
                    success: function (data) {
                        },
                    error: function (data) {
                        TestAlert("Error");
                    }
                });
    
    链接地址: http://www.djcxy.com/p/8086.html

    上一篇: 在jQuery中序列化为JSON

    下一篇: 如何从JSON对象构建JSON字符串