dongzhuo1733 2014-03-13 03:55
浏览 161
已采纳

删除json_encode / PHP中的双引号

I need some help with JSON and PHP. Here's my code in PHP:

include 'class.Connection.php';

$branch = $_GET["b"];   
$records = array();

$sqlNailDisplay = "SELECT NAD_ID FROM tbl_NailArtDesign WHERE NAD_Available = 1";
$query0 = mysql_query($sqlNailDisplay) or die(mysql_error());
while($rSet0 = mysql_fetch_array($query0, MYSQL_BOTH)) {

    $actualPrice = 0.00;
$nailart = $rSet0["NAD_ID"];
//please note, { is the ascii code for '{', } is the ascii code for '}', while " is the ascii code for '"'
$mergedData = "{"NAD_ID":"".$nailart."","";

//individual nail art details
$sqlNailArt = "SELECT * FROM tbl_NailArtDesign WHERE NAD_ID = '".$nailart."' AND NAD_Available = 1";
$query1 = mysql_query($sqlNailArt) or die(mysql_error());
while($rSet1 = mysql_fetch_array($query1, MYSQL_BOTH)) {
    $NAD_Ext = $rSet1["NAD_Ext"];
    $CC_ID = $rSet1["CC_ID"];
    $CT_ID = $rSet1["CT_ID"];
    $CST_ID = $rSet1["CST_ID"];
    if(empty($CST_ID)) {
    $CST_ID = "null";
    }
    $NAD_Descrip = $rSet1["NAD_Descrip"];

    $mergedData = $mergedData."NAD_Ext":"".$NAD_Ext."","CC_ID":"".$CC_ID."","CT_ID":"".$CT_ID."","CST_ID":"".$CST_ID."","NAD_Descrip":"".$NAD_Descrip."","";
    }

//product used and price details
$sqlProductsUsed = "SELECT PL_ID FROM tbl_ProductUsed WHERE NAD_ID = '".$nailart."'";
$query2 = mysql_query($sqlProductsUsed) or die(mysql_error());
while($rSet2 = mysql_fetch_array($query2, MYSQL_BOTH)) {
    $PL_ID = $rSet2["PL_ID"];
    $sqlProductPrice = "SELECT PP_Amount FROM tbl_ProductPrice WHERE PL_ID = ".$PL_ID." AND BL_ID = '".$branch."'";
    $query3 = mysql_query($sqlProductPrice) or die(mysql_error());
    while($rSet3 = mysql_fetch_array($query3, MYSQL_BOTH)) {
    $price = number_format($rSet3["PP_Amount"],2);
    $actualPrice = number_format($actualPrice + $price,2);
    }           
    $mergedData = $mergedData."PL_ID":"".$PL_ID."","PP_Amount":"".$price."","";
}
$mergedData = $mergedData."NAD_Price":"".$actualPrice.""}";

$records[] = $mergedData;

} mysql_free_result($query0);

echo json_encode($records);

And this is the result I'm getting:

["{"NAD_ID":"ND0001","NAD_Ext":"jpg","CC_ID":"1","CT_ID":"1","CST_ID":"null","NAD_Descrip":"Giving you the aquatic feeling with Turquoise Marble","PL_ID":"1","PP_Amount":"9.00","PL_ID":"2","PP_Amount":"9.10","PL_ID":"3","PP_Amount":"9.00","NAD_Price":"27.10"}","{"NAD_ID":"ND0002","NAD_Ext":"jpg","CC_ID":"1","CT_ID":"1","CST_ID":"null","NAD_Descrip":"Add a twirl in your life with Lavender Twirl","PL_ID":"1","PP_Amount":"9.00","PL_ID":"2","PP_Amount":"9.10","PL_ID":"3","PP_Amount":"9.00","NAD_Price":"27.10"}"]

I need my result to look like this:

[{"NAD_ID":"ND0001","NAD_Ext":"jpg","CC_ID":"1","CT_ID":"1","CST_ID":"null","NAD_Descrip":"Giving you the aquatic feeling with Turquoise Marble","PL_ID":"1","PP_Amount":"9.00","PL_ID":"2","PP_Amount":"9.10","PL_ID":"3","PP_Amount":"9.00","NAD_Price":"27.10"},{"NAD_ID":"ND0002","NAD_Ext":"jpg","CC_ID":"1","CT_ID":"1","CST_ID":"null","NAD_Descrip":"Add a twirl in your life with Lavender Twirl","PL_ID":"1","PP_Amount":"9.00","PL_ID":"2","PP_Amount":"9.10","PL_ID":"3","PP_Amount":"9.00","NAD_Price":"27.10"}]

There an extra double quotes that I need to remove from my output.

["{" , ",*"* , }"]

Please help, I'm already at my limit and I already did searching for this, and I can't seem to get any resolution for this...

  • 写回答

1条回答 默认 最新

  • doulu6314 2014-03-13 04:00
    关注

    Hardcoded method:

    $result = "[".substr(json_encode($records), 2, -2)."]";
    $result = str_replace('","', ',', $result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?