php生成数组转成json后,在转化为对象
//障碍物坐标指定设置varablockxy='{"xy":['+'{"left":"0","top":"0"},'+'{"left":"20","top":"0"},'...
//障碍物坐标指定设置
var ablockxy = '{ "xy" : [' +
'{ "left":"0" , "top":"0" },' +
'{ "left":"20" , "top":"0" },' +
'{ "left":"40" , "top":"0" },' +
'{ "left":"60" , "top":"0" },' +
'{ "left":"80" , "top":"0" },' +
'{ "left":"100" , "top":"0" },' +
'{ "left":"120" , "top":"0" },' +
'{ "left":"140" , "top":"0" },' +
'{ "left":"160" , "top":"0" },' +
'{ "left":"180" , "top":"0" },' +
'{ "left":"200" , "top":"0" },' +
'{ "left":"220" , "top":"100" } ]}';
var obj = eval("(" + ablockxy + ")");
var xy = obj.xy;
oblock.style.left = xy[i].left+'px';
oblock.style.top = xy[i].top+'px';
现在想从数据库中读取数据生成var ablockxy
while ($row=mysql_fetch_array($query)) {
$arr[] = array(
'left'=>$row['lv1_left'],
'top'=>$row['lv1_top']
);
}
echo json_encode($arr); //转换为json数据输出
到这里怎么做才能生成 var ablockxy ,然后读取其中数据 展开
var ablockxy = '{ "xy" : [' +
'{ "left":"0" , "top":"0" },' +
'{ "left":"20" , "top":"0" },' +
'{ "left":"40" , "top":"0" },' +
'{ "left":"60" , "top":"0" },' +
'{ "left":"80" , "top":"0" },' +
'{ "left":"100" , "top":"0" },' +
'{ "left":"120" , "top":"0" },' +
'{ "left":"140" , "top":"0" },' +
'{ "left":"160" , "top":"0" },' +
'{ "left":"180" , "top":"0" },' +
'{ "left":"200" , "top":"0" },' +
'{ "left":"220" , "top":"100" } ]}';
var obj = eval("(" + ablockxy + ")");
var xy = obj.xy;
oblock.style.left = xy[i].left+'px';
oblock.style.top = xy[i].top+'px';
现在想从数据库中读取数据生成var ablockxy
while ($row=mysql_fetch_array($query)) {
$arr[] = array(
'left'=>$row['lv1_left'],
'top'=>$row['lv1_top']
);
}
echo json_encode($arr); //转换为json数据输出
到这里怎么做才能生成 var ablockxy ,然后读取其中数据 展开
1个回答
展开全部
你可以参考一下我的建议,1.普通:若php关联数组不含有中文,直接json_encode转换。转换之后用json_deconde($stuJson)转为php对象。
<?php
$stu = array("name"=>"li","age"=>"35");
$stuJson = json_encode($stu);
还有什么疑问的话,可以多去后盾们看一下相关的视频,这样你更清楚
<?php
$stu = array("name"=>"li","age"=>"35");
$stuJson = json_encode($stu);
还有什么疑问的话,可以多去后盾们看一下相关的视频,这样你更清楚
追问
var arr = JSON.stringify();//json数组转json字符串
var xy = JSON.parse(arr);//json字符串转json对象
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询