如何将多维json转换为嵌套的POST字段

时间:2021-03-28 21:28:16

Is there any plugin or function that converts multi-dimensional JSON like this:

是否有任何插件或函数可以像这样转换多维JSON:

{
    "hello" : {
        "foo" : "bar",
        "arr" : ["a", "b", "c"]
    },
    "another": {
        "go" : {
            "very" : {                
                "deep" : 1                
            }
        }
    }
}

To array in this format

以这种格式排列

[
    {"key" : "another[go][very][deep]", "value" : "1"),
    {"key" : "hello[arr][]", "value" :a"),
    {"key" : "hello[arr][]", "value" :b"),
    {"key" : "hello[arr][]", "value" :c"),
    {"key" : "hello[foo]", "value" :bar")
]

Or do I need to write it at my own? Forgive me if I am wrong but when jQuery makes ajax call input JSON has to be converted to data in format above?

或者我需要自己写吗?原谅我,如果我错了,但是当jQuery使ajax调用输入JSON必须转换为上面格式的数据?

I am trying to create function/plugin that creates form with hidden fields to be sent into <iframe>

我正在尝试创建函数/插件,创建带有隐藏字段的表单以发送到

So basically function like this but that allows multi-dimensional params

所以基本上这样的功能,但允许多维参数

1 个解决方案

#1


0  

Ok I made my custom function:

好的,我做了自定义功能:

$.postirify = function(obj) {
    var tmp = $.param(obj).split("&");
    console.log(tmp);
    var result = [];
    $.each(tmp, function(i, v) {
        var kv = v.split("=");        
        result.push({
            key: decodeURIComponent(kv[0]),
            value: decodeURIComponent(kv[1])
        });
    });
    return result;
}

Demo: http://jsfiddle.net/9wL9zz8L/

And in the result I can create my form with hidden data that can be submitted to <iframe>

在结果中,我可以使用隐藏数据创建我的表单,可以将其提交到

$.fn.hiddenForm = function(data) {        
    var $form = $(this);
    $form.html("");
    var p = $.postirify(data);
    $.each(p, function(i, kv){
        var $input = $('<input/>');
        $input.attr("type", "hidden");
        $input.attr("name", kv.key);
        $input.val(kv.value);
        $form.append($input);
    });
    return $form;
};

$('#my-form').hiddenForm($.postirify(my_data)).submit();

#1


0  

Ok I made my custom function:

好的,我做了自定义功能:

$.postirify = function(obj) {
    var tmp = $.param(obj).split("&");
    console.log(tmp);
    var result = [];
    $.each(tmp, function(i, v) {
        var kv = v.split("=");        
        result.push({
            key: decodeURIComponent(kv[0]),
            value: decodeURIComponent(kv[1])
        });
    });
    return result;
}

Demo: http://jsfiddle.net/9wL9zz8L/

And in the result I can create my form with hidden data that can be submitted to <iframe>

在结果中,我可以使用隐藏数据创建我的表单,可以将其提交到

$.fn.hiddenForm = function(data) {        
    var $form = $(this);
    $form.html("");
    var p = $.postirify(data);
    $.each(p, function(i, kv){
        var $input = $('<input/>');
        $input.attr("type", "hidden");
        $input.attr("name", kv.key);
        $input.val(kv.value);
        $form.append($input);
    });
    return $form;
};

$('#my-form').hiddenForm($.postirify(my_data)).submit();