Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate JSON without slashes using PHP & MySQL

Tags:

json

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.

like image 831
Youssef Al Subaihi Avatar asked May 15 '26 08:05

Youssef Al Subaihi


1 Answers

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.

like image 187
Jonathan Kuhn Avatar answered May 16 '26 22:05

Jonathan Kuhn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!