dongxia4880
2017-03-20 20:10
浏览 33

我在php中需要json响应方面的帮助

Here is my code:

<?php
while ($row = mysqli_fetch_assoc($searching_user))
{
    $salon_name = ucfirst($row['service_name']);
    $salon_id = ucfirst($row['id']);
    $salon_address = ucwords($row['address']);
    $salon_area = ucwords($row['area']);
    $salon_city = ucwords($row['city']);
    $salon_specialty = ucwords($row['specialty']);
    $img = $row['image_url'];
    $response["error"] = FALSE;
    $response["service_name"] = $salon_name;
    echo json_encode($response);
}
?>

after this I'm getting the response in this format

{"error":false,"service_name":"Mike salon"}{"error":false,"service_name":"Michel salon"} {"error":false,"service_name":"Michel salon"}{"error":false,"service_name":"Mike Salon"} {"error":false,"service_name":"Etta Salon"}

I simply want this response like this

[ {"error":false,"service_name":"Mike salon"},{"error":false,"service_name":"Michel salon"},{"error":false,"service_name":"Michel salon"},{"error":false,"service_name":"Mike Salon"}, {"error":false,"service_name":"Etta Salon"}]

Kindly help me to get a proper response form for json . Thanks

图片转代码服务由CSDN问答提供 功能建议

这是我的代码:

 <  code>&lt;?php 
while($ row = mysqli_fetch_assoc($ searching_user))
 {
 $ salon_name = ucfirst($ row ['service_name']); 
 $ salon_id = ucfirst($ row ['id'  ]); 
 $ salon_address = ucwords($ row ['address']); 
 $ salon_area = ucwords($ row ['area']); 
 $ salon_city = ucwords($ row ['city'])  ; 
 $ salon_specialty = ucwords($ row ['specialty']); 
 $ img = $ row ['image_url']; 
 $ response [“error”] = FALSE; 
 $ response [“service_name”  ] = $ salon_name; 
 echo json_encode($ response); 
} 
?&gt; 
   
 
 

在此之后我收到回复 以这种格式

{“error”:false,“service_name”:“Mike salon”} {“error”:false,“service_name “:”Michel salon“} {”error“:false,”service_name“:”Michel salon“} {”error“:false,”service_name“:”Mike Salon“} {”error“:false, “service_name”:“Etta Salon”}

我只想要这个 赞成这样

[{“error”:false,“service_name”:“Mike salon”},{“error”:false, “service_name”:“Michel salon”},{“error”:false,“service_name”:“Michel salon”},{“error”:false,“service_name”:“Mike Salon”}, {“error”:false,“service_name”:“Etta Salon”}]

请帮助我为json获取正确的响应表单。 谢谢

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doubeizhong5178 2017-03-20 20:17
    已采纳

    You are trying to encode single results, Try to create a array will all the results and encode it out side the loop.

    while ($row=mysqli_fetch_assoc($searching_user)) {
        $salon_name = ucfirst($row['service_name']);
         $salon_id = ucfirst($row['id']);
         $salon_address = ucwords($row['address']);
         $salon_area = ucwords($row['area']);
         $salon_city = ucwords($row['city']);
         $salon_specialty = ucwords($row['specialty']);
         $img = $row['image_url'];
         $response["error"] = FALSE;
         $response["service_name"]=$salon_name;
         // Added this line
         $responses[] = $response;
    }
    //Encode all results
     echo json_encode($responses );
    
    已采纳该答案
    打赏 评论
  • dpsyssiv90846 2017-03-20 20:16

    Don't json_encode() the single results, but put them into an array and finally json_encode() that:

    <?php
    $response = [];
    while ($row=mysqli_fetch_assoc($searching_user)) {
      $salon_name = ucfirst($row['service_name']); 
      $salon_id = ucfirst($row['id']);
      $salon_address = ucwords($row['address']);
      $salon_area = ucwords($row['area']);
      $salon_city = ucwords($row['city']);
      $salon_specialty = ucwords($row['specialty']);
      $img = $row['image_url'];
    
      $response[] = [
        'error' => FALSE,
        'service_name' => $salon_name,
        // you may want to add more attributes here...
      ];
    }
    echo json_encode($response);
    

    I personally suggest to shorten this:

    <?php
    $response = [];
    while ($row=mysqli_fetch_assoc($searching_user)) {
      $response[] = [
        'error'           => FALSE,
        'service_name'    => ucfirst($row['service_name']),
        'salon_id'        => $row['id'],
        'salon_address'   => ucwords($row['address']),
        'salon_area'      => ucwords($row['area']),
        'salon_city'      => ucwords($row['city']),
        'salon_specialty' => ucwords($row['specialty']),
        'img'             => $row['image_url'],
      ];
    }
    echo json_encode($response);
    
    打赏 评论

相关推荐 更多相似问题