laravel 将多维表单数据序列化为 JSON 对象数组以与 application/json 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32686855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serialize multidimensional form data into a JSON object-array to work with application/json
提问by Josh
I am building a Laravel application. In the back-end, the $controller->wantsJson()
method is TRUE
if the content type of the request is application/json
. So, to satisfy this, my jQuery AJAX call is this.
我正在构建一个 Laravel 应用程序。在后端,$controller->wantsJson()
方法是TRUE
如果请求的内容类型是application/json
。所以,为了满足这一点,我的 jQuery AJAX 调用是这样的。
jQuery.ajax({
type: "POST",
method: "PUT",
url: $form.attr('action'),
data: $form.serialize(),
dataType: "json",
contentType: "application/json; charset=utf-8"
})
This correctly triggers the wantsJson()
response I need. The problem then is that jQuery cannot supply POST data as an application/json
correctly. It tries to write it as a query string, which doesn't work for the PHP backend when it receives that content type.
这正确触发了wantsJson()
我需要的响应。问题是 jQuery 无法application/json
正确提供 POST 数据。它尝试将其编写为查询字符串,当它接收到该内容类型时,该字符串不适用于 PHP 后端。
To satisfy this, I need to stringify a JavaScript object-array into the data
field.
为了满足这一点,我需要将一个 JavaScript 对象数组字符串化到该data
字段中。
JSON.stringify(dataObj)
So now the problem is, I don't have a concise way of turning a multidimensional form into an object-array. If I just try to convert the output of $.serializeArray()
, I get this, which isn't able to be interpreted by the backend.
所以现在的问题是,我没有将多维形式转换为对象数组的简洁方法。如果我只是尝试转换 的输出$.serializeArray()
,我会得到这个,后端无法解释。
{
'something' : 1,
'field[foo]' : 2,
'field[bar][]' : "a",
'field[bar][]' : "b",
'field[bar][]' : "c",
}
And that of course doesn't work. I need some sort of RegEx parser that can convert this form ...
这当然行不通。我需要某种可以转换这种形式的正则表达式解析器......
<input name="something" value="1" />
<input name="field[foo]" value="2" />
<input name="field[bar][]" value="a" />
<input name="field[bar][]" value="b" />
<input name="field[bar][]" value="c" />
Into ...
进入 ...
{
'something' : 1,
'field' : {
'foo' : 2,
'bar' : [
'a',
'b',
'c',
]
}
}
You'd expect something like this to exist already, but I can't find anything satisfactory.
你会期望这样的东西已经存在,但我找不到任何令人满意的东西。
回答by mvpasarel
I had the same problem but this package did the trick: jQuery Serialize Object
我有同样的问题,但这个包成功了:jQuery Serialize Object
It's easy to use, just call this on your form (after loading the script)
它易于使用,只需在您的表单上调用它(加载脚本后)
$('form#contact').serializeJSON(); // to get the form as a JS object
回答by mdamia
You can use PHP parse_str
你可以使用 PHP parse_str
parse_str — Parses the string into variables
parse_str — 将字符串解析为变量
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
Hope this help.
希望这有帮助。
回答by khadkamhn
I agree with @mdamia Here is the snippet with more detail.
我同意@mdamia 这是更详细的片段。
After JSON.stringify(dataObj);
You will get a json string as below:
之后JSON.stringify(dataObj);
你会得到一个json字符串,如下所示:
JSON:
JSON:
[
{
"name":"something",
"value":"1"
},
{
"name":"field[foo]",
"value":"2"
},
{
"name":"field[bar][]",
"value":"a"
},
{
"name":"field[bar][]",
"value":"b"
},
{
"name":"field[bar][]",
"value":"c"
}
]
PHP Code:
PHP代码:
<?php
//your raw json string
$rawData = '[{"name":"something","value":"1"},{"name":"field[foo]","value":"2"},{"name":"field[bar][]","value":"a"},{"name":"field[bar][]","value":"b"},{"name":"field[bar][]","value":"c"}]';
$json = json_decode($rawData);
$string = '';
foreach($json as $key => $val){
$string .= $val->name.'='.$val->value.'&';
}
parse_str(rtrim($string,'&'),$data);
//var_dump($data);
echo json_encode($data); //your requied data
?>
//Output
//输出
{
"something":"1",
"field":{
"foo":"2",
"bar":[
"a",
"b",
"c"
]
}
}