转换mysql查询介绍json对象

时间:2016-11-15 15:16:53

标签: php mysql json

这是我的代码:

$eventList = array();

while($rows = mysql_fetch_array($produse)){

$url   = $http_location . '/' . str_replace(' ', '-', $rows['title']) . '.html' ;   
$image = ($rows['image_path'][0] == "." ? $http_location.substr($rows['image_path'], 1) : $r['image_path']);

$eventList[] = array(
    //'id' => $rows['productid'],
    'Title' => $rows['productname'],
    'Description' => $rows['description'],
    'Short message' => '',
    'Price' => $rows['price'],
    'Category' => $rows['category'],
    'Subcategory' => '',
    'URL' => $url,
    'Image' => $image,
    'Product ID' => $rows['productid'],
    'Generate link text' => 0,
    'Brand' => $rows['manufacturer'],
    'Active' => 1,
    'Other data' => ''
);

unset($url, $image);
}


// print '<pre>';
// print_r($eventList);

//header('Content-Type: application/json');

$eventList = json_encode($eventList);

echo json_encode($eventList);

mysql_close(); 

这是查询的结果:

  

“[{\”Title \“:\”Costum buburuza \“,\”Description \“:\”Costum buburuza copii pentru fete,包括rochia cu bretele din lycra,cu aplicatie floare,fuste din satin imprimat si tull pe interior,maneci detasabile,aripi si cordeluta cu antenute。\“,\”短信\“:\”\“,\”Price \“:\”119.00 \“,\”类别\“:\”Costume Carnaval \ “\ ”子类别\“:\ ”\“ \ ”URL \“:\ ”HTTP:\ / \ / www.fabricademagie.ro \ /Costum-buburuza-copii-fete.html \“,\” 图像\ “:\”http:\ / \ / www.fabricademagie.ro \ / images \ / P \ /63442-01.jpg \“,\”产品ID \“:\”5 \“,\”生成链接文字\ “:0,\”Brand \“:null,\”Active \“:1,\”Other data \“:\”\“},{\”Title \“:\”Costum buburuza \“,\”Description \“:\”Costum buburuza copii pentru fete,包括rochia cu bretele din lycra,cu aplicatie floare,fuste din satin imprimat si tull pe interior,maneci detasabile,aripi si cordeluta cu antenute。\“,”短信“: \“\”,\“Price \”:\“114.00 \”,\“Category \”:\“Costume Carnaval \”,\“Subcategory \”:\“\”,\“URL \”:\“http :\ / \ / www.fabricademagie.ro \ /Costum-buburuza-copii-fete.html \ “,”“图片\”:\“http:\ / \ / www.fabricademagie.ro \ / images \ / P \ /63442-01.jpg \”,\“产品ID \”:\“5 \”, \“生成链接文本\”:0,\“Brand \”:null,\“Active \”:1,\“其他数据\”:\“\”}]“

我做错了什么而且我记得这种类型的结果而不是标准的json对象?

3 个答案:

答案 0 :(得分:3)

$eventList = json_encode($eventList);
echo json_encode($eventList);

你两次编码字符串......

$eventList = json_encode($eventList);
echo $eventList;

TADA;)

答案 1 :(得分:1)

尝试删除一个json_encode:

$eventList = json_encode($eventList);

echo $eventList;

echo json_encode($eventList);

答案 2 :(得分:1)

从代码中删除第二个json_encode()函数。它应该是这样的:

$eventList = json_encode($eventList);
echo $eventList;

或者,如上所述:

echo json_encode($eventList);

将所有mysql()函数替换为mysqli或PDO或使用它们的库也是明智之举。