how can I access my json encoded data from PHP into a mysql query
I have been on a long mission here, I have gotten to my last step in my program and I can't seem to figure out how to crack open a JSON string from my Ajax request sent to my PHP.
My Jquery code looks like this;
$('#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;
});
and my PHP is:
if($_POST['save-import']){
$scenes = json_decode($_POST['data']);
echo($scenes);
}
A snippet of what my JSON response looks like:
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"}
when the ajax is sent to my php to get processed i need to be able to break apart the json response to accomplish this query
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')");
what the JSON should read in english is
scene: 24
int_ext: "INT"
desc: "LIVING ROOM"
day_night: "NIGHT"
as one query, and do to that for every scene in the JSON string.
EDIT:
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\\\"}
EDIT: with 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
The following worked for me. Apparently there is really something going wrong with the slashes there:
$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
)
*/
}
Based on the var_dump you posted, try this:
if($_POST['save-import']){
$scenes = json_decode(stripslashes($_POST['data']));
echo($scenes);
}
Looks like something is going a little crazy with escaping the JSON object before it gets transmitted.
精彩评论