2017-01-23 84 views
0

这是我得到的结果,而我试图以JSON格式编码字符串。我想要的网址应该是这样的:http://ipaddress/Up/user_images/549279.jpgJSON字符串与图像url或JSON格式的url编码

结果:

{ “结果”:[{ “ID”: “42”, “名”:“HTTP:// IP地址/上/ user_images/380289.jpg “},{” ID “:” 43" , “姓名”: “HTTP://IPADDRESS/Up/user_images/995062.jpg”},{ “ID”: “44”, “姓名” :“http://IPADDRESS/Up/user_images/171544.jpg”},{“id”:“41”,“name”:“http://IPADDRESS/Up/user_images/549279.jpg”}]}

警告:json_decode()预计参数1是串,阵列中的C中给出:\ XAMPP \ htdocs中\ JSN \ PHP_Scripts \上线getAllEmp.php 26

//creating a blank array 
$result = array(); 
$r1 = array(); 

//looping through all the records fetched 
while ($row = mysqli_fetch_array($r)){ 
    //Pushing name and id in the blank array created 
    array_push($result,array(
     "id"=>$row['userID'], 
     "name"=>$ur.$row['userPic'], 
    )); 
} 

//Displaying the array in json format 
echo json_encode(array('result'=>$result)); 
$r1= json_decode($result, true); 
echo ($r1); 
// echo json_decode(array("result"=>$r1)); 

mysqli_close($con); 
+0

http://stackoverflow.com/questions/4319105/remove-trailing-slash-from-string-php指这个,这将有助于 – Anjali

回答

0

试试下面的代码

//creating a blank array 
    $result = array(); 
    $r1=array(); 
    //looping through all the records fetched 
    while($row = mysqli_fetch_array($r)){ 
     //Pushing name and id in the blank array created 
      array_push($result,array(
      "id"=>$row['userID'], 
      "name"=>$ur.$row['userPic'], 
     )); 
    } 
    //Displaying the array in json format 
    $jsonData = json_encode($result); 
    echo $jsonData; // **MODIFIED LINE** 
    $r1 = json_decode($jsonData, true); // **MODIFIED LINE** 
    print_r($r1); 
    mysqli_close($con); 
+0

我们没有得到导致JSON格式。我们得到的结果与以前相同,rtrim也不起作用 – Vinny

+0

感谢您的回放,我找到了答案'$ jsonData = json_encode($ result,JSON_UNESCAPED_SLASHES); \t echo $ jsonData;' – Vinny