2014-09-22 51 views
1

我要生成的JSON值正是像这样一个在这里生成JSON不使用PHP和MySQL

{ 
    "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和网址 我希望我的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); 

?> 

我试图解决方案的很多在这里计算器,我没有找到任何一个谁可以接近我的问题。

+0

将类型转换为int类型的id值赋给$ id =(int)$ id; – 2014-09-22 18:36:43

+0

首先为什么它应该是这样的**完全**? – 2014-09-22 18:37:26

+0

你需要看看为编码http://php.net/manual/en/json.constants.php添加选项,至少将帮助使用JSON_UNESCAPED_SLASHES – 2014-09-22 18:38:29

回答

2

使用(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); 

?> 

希望这会有所帮助。

+0

太棒了:D工作,但URL的 – 2014-09-22 18:41:05

+0

刚刚编辑答案。 – worldofjr 2014-09-22 18:41:51

+0

谢谢你真棒,将在5分钟内做出正确答案:) – 2014-09-22 18:43:51

2

您正在使用mysql_query()(您不应该这样做,因为它已被弃用)。问题在于你没有办法将列绑定到值类型(字符串,整数等)。一切都以字符串形式返回。

我建议使用mysqliPDO,您可以在绑定它们时指定列类型。没有这个,你需要在编码为JSON之前手动转换你的值。

+0

thx为笔记亲,但im在这里问关于JSON,而不是关于MySql :) – 2014-09-22 18:43:25

+0

@YoussefAlSubaihi是的,但你的问题在于,特别是当用MySQL_query()所有的值从MySQL读取数据的事实被视为串。您需要理解这一点,根据预期实际构建您的数据结构并将其序列化(在本例中为JSON),而不是像在序列化过程中尝试处理数据一样其他答案。 – 2014-09-22 18:45:56

+0

所以你说的是,如果我使用mysqli或pdo,我将不需要使用JSON_UNESCAPED_SLASHES?或''(int)$ r ['id'];''? – 2014-09-22 18:48:09

1

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

json_encode($array, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK) 

你可以在手册页中找到所有这些信息,以获取json_encode

2

尝试下面的例子,如果你使用的是PHP 5.4+

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

对于较低的版本,你可以你的阵列,从而获得所需结果强制类型转换值。