是否有任何插件或函数可以像这样转换多维JSON:
{
"hello" : {
"foo" : "bar",
"arr" : ["a", "b", "c"]
},
"another": {
"go" : {
"very" : {
"deep" : 1
}
}
}
}
以这种格式排列
[
{"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")
]
或者我需要自己写吗?请原谅我,如果我错了,但是当jQuery调用ajax时,JSON必须转换为上面格式的数据?
我正在尝试创建一个函数/插件,用于创建带有隐藏字段的表单,以便发送到<iframe>
所以基本上像this这样的功能但允许多维参数
答案 0 :(得分:0)
好的,我做了自定义功能:
$.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;
}
演示:http://jsfiddle.net/9wL9zz8L/
在结果中,我可以创建包含可以提交到<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();