这是一个简单的问题..我试图使用传单实时库,但它需要加载特定的JSON输出,例如:
data: {"type":"Feature","geometry":{"type":"Point","coordinates":[-85.26995166666667,35.056891]},"properties":{"color":"#FFFFFF","route":"U"},"id":"118"}
到目前为止,这就是我得到的全部内容:
[{"type":"Feature","coordinates":["-34.66159","-59.42428"],"color":"#FFFFFF","route":"u", id:118}]
这是我的PHP
$id=$row['id'];
$type=$row['type'];
$lat=$row['lat'];
$lng=$row['lng'];
$color=$row['color'];
$route=$row['route'];
$data [] = array('id'=> $id, 'type'=> $type,'coordinates'=> $lnglat = array ($lat, $lng),
'colour'=> $colour, 'route'=> $route);
$json_string = json_encode($data);
echo $json_string;
这让我发疯了......我尝试了一切..我读了很多json_encode教程..但是我找不到像我这样的例子或情况。
请帮忙!
谢谢!
答案 0 :(得分:3)
尝试解码您需要创建的json
以查看PHP
数据结构应如何显示:
$json='{"type":"Feature","geometry":{"type":"Point","coordinates":[-85.26995166666667,35.056891]},"properties":{"color":"#FFFFFF","route":"U"},"id":"118"}';
$data = json_decode($json, TRUE);
print_r($data);
输出结果为:
Array
(
[type] => Feature
[geometry] => Array
(
[type] => Point
[coordinates] => Array
(
[0] => -85.269951666667
[1] => 35.056891
)
)
[properties] => Array
(
[color] => #FFFFFF
[route] => U
)
[id] => 118
)
现在,您的代码应如下所示:
$data = array(
'type' => $type,
'geometry' => array(
'type' => 'Point',
'coordinates' => array($lat, $lng),
),
'properties' => array(
'color' => $color,
'route' => $route,
),
'id' => $id,
);
echo(json_encode($data));
更新:,因为@ paul-crovella在评论中发表评论,在代码的第一个片段中使用var_export()
代替print_r()
直接生成与代码非常相似的PHP代码上面显示的(变量/值除外)可以复制粘贴到您的代码中,并通过将值('Features'
,35.056891
,'#FFFFFF'
aso)替换为相应的变量来使用( $type
,$lng
,$color
等。
答案 1 :(得分:0)
你试过这个:
$id=$row['id'];
$type=$row['type'];
$lat=$row['lat'];
$lng=$row['lng'];
$color=$row['color'];
$route=$row['route'];
$data [] = array('id'=> $id, 'type'=> $type,'coordinates'=> $lnglat = array ($lat, $lng),
'colour'=> $colour, 'route'=> $route);
$json_string = json_encode(array('data'=>$data));
echo $json_string;
我所做的只是在array()
中添加一个级别:
$json_string = json_encode($data);
进入这个:
$json_string = json_encode(array('data'=>$data));
$data['data'] = array('id'=> $id, 'type'=> $type,'coordinates'=> $lnglat = array ($lat, $lng),
'colour'=> $colour, 'route'=> $route);
$json_string = json_encode($data);
echo $json_string;
OR
$data = array('id'=> $id, 'type'=> $type,'coordinates'=> $lnglat = array ($lat, $lng),
'colour'=> $colour, 'route'=> $route);
$json_string = json_encode(array('data'=>$data));
echo $json_string;
答案 2 :(得分:0)
你可以试试这个:
$id = 1;
$type = "type";
$lat = 42.565;
$lng = 19.6464;
$colour = "Color";
$route = "Route";
$data = array('id' => $id, 'type' => $type, 'coordinates' => $lnglat = array($lat, $lng),
'colour' => $colour, 'route' => $route);
$json_string = json_encode(array('data' => $data));
echo $json_string;
从[]
删除$data
。
输出是:
{"data":{"id":1,"type":"type","coordinates":[42.565,19.6464],"colour":"Color","route":"Route"}}
答案 3 :(得分:0)
这对你有用:
$dataArray = array('id'=> $id, 'type'=> $type,'coordinates'=> $lnglat = array ($lat, $lng), 'colour'=> $colour, 'route'=> $route);
$json_string = json_encode(array('data'=>$dataArray));
echo $json_string;
答案 4 :(得分:0)
您对$data
数组的分配应如下所示:
$data['data'] = array(
'id' => $id,
'type' => $type,
'coordinates' => $lnglat = array($lat, $lng),
'colour' => $color,
'route' => $route
);
P.S。您在$color
变量中也有拼写错误。