作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这里完成了一项长期任务,我已经到了我程序的最后一步,但我似乎无法弄清楚如何从发送到我的 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\\\"}
编辑:带斜线
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
最佳答案
以下对我有用。显然那里的斜杠确实有问题:
$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
)
*/
}
关于php - 如何从 PHP 访问我的 json 编码数据到 mysql 查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5226059/
我是一名优秀的程序员,十分优秀!