我在这里执行了很长时间的任务,我已经完成了我的程序中的最后一步,我似乎无法弄清楚如何从发送到我的PHP的Ajax请求中打开一个JSON字符串。
我的Jquery代码看起来像这样;
$('#formElem').submit(function(){
var data = {};
$("input[name=scene]").each(function() {
data[$(this).val()] = {
scene: $(this).val(),
int_text: $(this).next("input[name=int_ext]").val(),
desc: $(this).next().next("input[name=scene_desc]").val(),
day_night: $(this).next().next().next("input[name=day_night]").val()
};
});
$.post("script.import.php", {
action: 'save-import',
data: data // as-is
//data: JSON.stringify(data) // with stringify
}, function(resp) {
console.log(eval('('+resp.scene+')'));
console.log(eval(resp.int_ext));
console.log(eval(resp.desc));
console.log(resp.day_night);
});
return false;
});
我的PHP是:
if($_POST['save-import']){
$scenes = json_decode($_POST['data']);
echo($scenes);
}
我的JSON响应的代码片段:
WITHOUT stringify()
action save-import
data[100][day_night] NIGHT
data[100][desc] SAPNA'S BEDROOM
data[100][int_text] INT
data[100][scene] 100
data[101][day_night] NIGHT
data[101][desc] LIVING ROOM
data[101][int_text] INT
data[101][scene] 101
data[102][day_night] NIGHT
data[102][desc] SHAH HOUSE, FRONT YARD
data[102][int_text] EXT
data[102][scene] 102
data[103][day_night] NIGHT
data[103][desc] SHAH HOUSE, FRONT PORCH
data[103][int_text] EXT
WITH stringify()
{"2":{"scene":"2","int_text":"INT","desc":"SAPNA'S BEDROOM","day_night":"MORNING"},"9":{"scene":"9","int_text":"INT","desc":"BMW","day_night":"NIGHT"},"19":{"scene":"19","int_text":"INT","desc":"SAPNA'S BEDROOM","day_night":"DAY"},"21":{"scene":"21","int_text":"INT","desc":"KITCHEN","day_night":"DAY"},"22":{"scene":"22","int_text":"INT","desc":"HALLWAY","day_night":"DAY"},"23":{"scene":"23","int_text":"INT","desc":"TEMPLE ROOM","day_night":"DAY"},"24":{"scene":"24","int_text":"INT","desc":"LIVING ROOM","day_night":"NIGHT"}
当ajax被发送到我的php进行处理时,我需要能够分解json响应来完成这个查询
mysql_query("INSERT INTO (`project_id`, `scene`, `int_ext`, `scene_desc`, `day_night`)
VALUES ('10', 'data.scene', 'data.int_ext', 'data.desc', 'data.day_night')");
JSON应该用英语读什么
scene: 24
int_ext: "INT"
desc: "LIVING ROOM"
day_night: "NIGHT"
作为一个查询,并对JSON字符串中的每个场景执行此操作。
编辑:
with var_dump
string(11463) ""{\\\"2\\\":{\\\"scene\\\":\\\"2\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"SAPNA\\'S BEDROOM\\\",\\\"day_night\\\":\\\"MORNING\\\"},\\\"9\\\":{\\\"scene\\\":\\\"9\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"BMW\\\",\\\"day_night\\\":\\\"NIGHT\\\"},\\\"19\\\":{\\\"scene\\\":\\\"19\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"SAPNA\\'S BEDROOM\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"21\\\":{\\\"scene\\\":\\\"21\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"KITCHEN\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"22\\\":{\\\"scene\\\":\\\"22\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"HALLWAY\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"23\\\":{\\\"scene\\\":\\\"23\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"TEMPLE ROOM\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"24\\\":{\\\"scene\\\":\\\"24\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"LIVING ROOM\\\",\\\"day_night\\\":\\\"NIGHT\\\"},\\\"26\\\":{\\\"scene\\\":\\\"26\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"TREE HOUSE\\\",\\\"day_night\\\":\\\"NIGHT\\\"}
编辑:使用stripslashes
Catchable fatal error: Object of class stdClass could not be converted to string in /home/content/06/5679006/html/v3/test.php on line 4
答案 0 :(得分:0)
根据您发布的var_dump,试试这个:
if($_POST['save-import']){
$scenes = json_decode(stripslashes($_POST['data']));
echo($scenes);
}
在传输JSON对象之前看起来有些疯狂。
答案 1 :(得分:0)
以下对我有用。显然,那里的斜线确实出现了问题:
$json = <<<JSON
{\\\"2\\\":{\\\"scene\\\":\\\"2\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"SAPNA\\'S BEDROOM\\\",\\\"day_night\\\":\\\"MORNING\\\"},\\\"9\\\":{\\\"scene\\\":\\\"9\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"BMW\\\",\\\"day_night\\\":\\\"NIGHT\\\"},\\\"19\\\":{\\\"scene\\\":\\\"19\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"SAPNA\\'S BEDROOM\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"21\\\":{\\\"scene\\\":\\\"21\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"KITCHEN\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"22\\\":{\\\"scene\\\":\\\"22\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"HALLWAY\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"23\\\":{\\\"scene\\\":\\\"23\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"TEMPLE ROOM\\\",\\\"day_night\\\":\\\"DAY\\\"},\\\"24\\\":{\\\"scene\\\":\\\"24\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"LIVING ROOM\\\",\\\"day_night\\\":\\\"NIGHT\\\"},\\\"26\\\":{\\\"scene\\\":\\\"26\\\",\\\"int_text\\\":\\\"INT\\\",\\\"desc\\\":\\\"TREE HOUSE\\\",\\\"day_night\\\":\\\"NIGHT\\\"}}
JSON;
$json = stripslashes(stripslashes($json));
$data = json_decode($json);
foreach($data as $item) {
/*
Item looks like this:
stdClass Object
(
[scene] => 2
[int_text] => INT
[desc] => SAPNA'S BEDROOM
[day_night] => MORNING
)
*/
}