这是我在尝试以JSON格式编码字符串时获得结果的方式。我希望该网址应该在:http://ipaddress/Up/user_images/549279.jpg
结果:
{"导致&#34 ;: [{" ID":" 42""名称":" HTTP:// IPADDRESS /上/ user_images / 380289.jpg"},{" ID":" 43""名称":" HTTP:// IPADDRESS /上/ user_images / 995062.jpg"},{" ID":" 44""名称":" HTTP:// IPADDRESS /上/ user_images / 171544.jpg"},{" ID":" 41""名称":" HTTP:// IPADDRESS / Up / user_images / 549279.jpg"}]}
警告:json_decode()期望参数1为字符串,第26行的C:\ xampp \ htdocs \ jsn \ PHP_Scripts \ getAllEmp.php中给出的数组
//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 :(得分: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);