dtcyv3985 2017-08-04 06:45
浏览 342

如何使用PHP删除JSON中的方形括号

Below code is the returned JSON

[{"one":"id","two":"id","three":"id"},{"one":"id","two":"id","three":"id"}]

Below code is the desired result of the returned JSON (without the array bracket)

{"one":"id","two":"id","three":"id"},{"one":"id","two":"id","three":"id"}

Below code is to convert the array to the JSON format

include('connect-db.php'); 
$result = mysql_query("SELECT * FROM patientvaccinedetail"); 
$specific = []; 
while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) { 
echo "<tr>"; 
echo '<td width="100px">' . $row['id'] . '</td>'; 
echo '<td width="200px">' . $row['patientid'] . '</td>'; 
echo '<td width="200px">' . $row['vaccineid'] . '</td>'; 
  //**********Convert the array into json******************* 

  $specific[] = ["one" => $row["id"],
                 "two" => $row["patientid"],
                 "three" => $row["vaccineid"]];

$result = json_encode($specific,JSON_UNESCAPED_UNICODE);

echo $result;
echo "</tr>";
echo "</table>";
?>

To send the request to the API, iam using Guzzle And the format the API require is {xx:xx},{xx:xx} without the square bracket, any idea how to remove it using PHP. Thanks in advance

     $client = new Client([
    'headers' => ['Content-Type' => 'application/json',
                  'Token' => $token]
                           ]);

       $response = $client->post('http://localhost:91/Religious',
       ['body' => ***Where the json will be place***]
       );
  • 写回答

6条回答 默认 最新

  • douyingyu5573 2017-08-04 07:01
    关注

    My suggestion is to use JSON string and perform String operations to remove all those not required characters.

    Reason for doing this, the required output is not a valid JSON and won't be accepted by API. As you need it however, I would suggest you to convert the array into json using json_encode and then perform string operations to convert it into your desired output.

    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了