简体   繁体   中英

Generate JSON without slashes using PHP & MySQL

i want to generate JSON values exactly like this one here

{
    "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"
        }
    ]
}

and every time i try to do such a thing i end up with this output

{
    "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"
    }]
}

look at the ID in the first json, and the URLs i want my json to be exactly like the first one

my php code :

<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);

?>

i tried a LOT of solutions here on stackoverflow and i didn't find any one who could be close to my problem.

Cast the ID value to integer using (int) , and escape the URL slashes with JSON_UNESCAPED_SLASHES ;

<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);

?>

Hope this helps.

You are using mysql_query() (which you shouldn't be doing since it is deprecated). The problem with this is that you have no way to bind columns to value types (string, integer, etc.). Everything is being returned as a string.

I would suggest using mysqli or PDO where you can specify column types when binding to them. Without this, you will need to manually cast your values before encoding to JSON.

Try following example if you are using PHP 5.4+

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

For lower versions you can type cast values in your array to get desired result.

If you have php 5.4+ you can use JSON_UNESCAPED_SLASHES and JSON_NUMERIC_CHECK to get the results you want.

json_encode($array, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK)

You can find all of this on the manual page for json_encode .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM