Generate JSON without slashes using PHP & MySQL


Generate JSON without slashes using PHP & MySQL

我想生成JSON值,就像这里的一样

{
    "feed": [
        {
            "id": 1,
            "name": "National Geographic Channel",
            "image": "http://api.androidhive.info/feed/img/cosmos.jpg",
            "status": "'"Science is a beautiful and emotional human endeavor,'" says Brannon Braga, executive producer and director. '"And Cosmos is all about making science an experience.'"",
            "profilePic": "http://api.androidhive.info/feed/img/nat.jpg",
            "timeStamp": "1403375851930",
            "url": null
        },
        {
            "id": 2,
            "name": "TIME",
            "image": "http://api.androidhive.info/feed/img/time_best.jpg",
            "status": "30 years of Cirque du Soleil's best photos",
            "profilePic": "http://api.androidhive.info/feed/img/time.png",
            "timeStamp": "1403375851930",
            "url": "http://ti.me/1qW8MLB"
        }
    ]
}

每次我尝试做这样的事情,我都会得到这个输出

{
    "feed": [{
        "id": "1",
        "name": "National Geographic Channel",
        "image": "http:'/'/api.androidhive.info'/feed'/img'/cosmos.jpg",
        "status": "Science is a beautiful and emotional human endeavor",
        "profilePic": "http:'/'/api.androidhive.info'/feed'/img'/nat.jpg",
        "timeStamp": "1403375851930",
        "url": "null"
    }, {
        "id": "2",
        "name": "National Geographic Channel",
        "image": "http:'/'/api.androidhive.info'/feed'/img'/cosmos.jpg",
        "status": "Science is a beautiful and emotional human endeavor",
        "profilePic": "http:'/'/api.androidhive.info'/feed'/img'/nat.jpg",
        "timeStamp": "1403375851930",
        "url": "null"
    }]
}

查看第一个json中的ID和URL我希望我的json和第一个完全一样

我的php代码:

<meta http-equiv="Content-Type" content="application/json; charset=utf-8 ">
<?php
include "../funcs/db.php";
$SelectPosts = mysql_query("SELECT * FROM usf_mobile");
$rows = array();
   while($r = mysql_fetch_assoc($SelectPosts)) {
     $rows['feed'][] = $r;
   }
 print json_encode($rows);
?>

我在stackoverflow上尝试了很多解决方案,但没有找到一个能解决我问题的人。

如果您有php 5.4+,您可以使用JSON_UNESCAPED_SLASHESJSON_NUMERIC_CHECK来获得您想要的结果。

json_encode($array, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK)

您可以在json_encode的手册页面上找到所有这些。

使用(int)将ID值强制转换为整数,并使用JSON_UNESCAPED_SLASHES转义URL斜杠;

<meta http-equiv="Content-Type" content="application/json; charset=utf-8 ">
<?php
include "../funcs/db.php";
$SelectPosts = mysql_query("SELECT * FROM usf_mobile");
$rows = array();
   while($r = mysql_fetch_assoc($SelectPosts)) {
     $r['id'] = (int)$r['id'];
     $rows['feed'][] = $r;
   }
 print json_encode($rows,JSON_UNESCAPED_SLASHES);
?>

希望这能有所帮助。

如果您使用的是PHP 5.4+,请尝试以下示例

 $rows = json_encode($rows,JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

对于较低版本,您可以在数组中键入强制转换值以获得所需的结果。

您使用的是mysql_query()(不应该这样做,因为它已被弃用)。这样做的问题是,您无法将列绑定到值类型(字符串、整数等)。所有内容都以字符串的形式返回。

我建议使用mysqliPDO,在绑定到它们时可以指定列类型。如果没有这一点,您将需要在编码为JSON之前手动强制转换您的值。