実現したいこと
API接続で、GETで値を受け取りSQL接続して値を取得した結果を配列に格納して、最後
print json_encode($arr, JSON_PRETTY_PRINT);
でjson形式にデコードしています。
しかし、配列の中に配列(変数名:$carosels)があり、その部分は利用先のサービスでJSON形式を文字列としたものを渡す予定なので、これをJSONデコードしたものを渡すとエラーとなり表示されませんでした。
これだけJSONにデコードせず、文字列で渡すにはどうすればよいでしょうか?
よろしくお願いいたします。
発生している問題・エラーメッセージ
エラーメッセージ
PHP
1header('Content-Type: application/json; charset=UTF-8'); 2 3// 検索 4if(isset($_GET["test"])){ 5 $keyword = strtoupper($_GET["test"]); 6 // DB接続定義読み込み 7 global $wpdb; 8 $object = $wpdb->get_results("SELECT * FROM xxxx WHERE 検索用 LIKE '%".$keyword."%' ORDER BY 発売日 ASC"); 9 10 $num = 0; 11 $results = []; 12 $carosels = []; 13 $buttons = []; 14 $contents = []; 15 $values = []; 16 17 $separator["type"] = "separator"; 18 $button1["type"] = "button"; 19 $button1["action"] = ["type"=>"message", "label"=>"この商品です","text"=>"この商品です"]; 20 $button2["type"] = "button"; 21 $button2["action"] = ["type"=>"message", "label"=>"もっと見る","text"=>"もっと見る"]; 22 $bubbules = []; 23 while($row = $object[$num]){ 24 25 $content0["type"] = "image"; 26 $content0["url"] = $row->xxx; 27 $content1["type"] = "text"; 28 $content1["text"] = $row->bbb; 29 $content2["type"] = "text"; 30 $content2["text"] = $row->zzz; 31 $conents = [$content0,$content1, $content2, $separator, $button1, $button2]; 32 $body["type"] = "box"; 33 $body["layout"] = "vertical"; 34 $body["contents"] = $conents; 35 $bubbule["type"] = "bubble"; 36 $bubbule["body"] = $body; 37 array_push($bubbules, $bubbule); 38 $num++; 39 } 40 if($num == 0){ 41 $arr["status"] = "no"; 42 $arr["hit_count"] = 0; 43 }else{ 44 45 $arr["status"] = "yes"; 46 $carosels["type"] = "carousel"; 47 $carosels["contents"] = $bubbules; 48 $arr["hit_count"] = $num; 49 // これだけJSON形式にしない 文字列で返したい 50 $arr["carousel"] = $carosels; 51 52 } 53 54} 55 56// 配列をjson形式にデコードして出力, 第二引数は、整形するためのオプション 57print json_encode($arr, JSON_PRETTY_PRINT); 58
試したこと
現状では、$carosels を出力させせると下記になります。
{"type"=>"carousel", "contents"=>[{"type"=>"bubble", "body"=>{"type"=>"box", "layout"=>"vertical", "contents"=>[{"type"=>"image", "url"=>"https://xxx/xxx.jpg"}, {"type"=>"text", "text"=>"xxxのxxx"}, {"type"=>"text", "text"=>"2020-07-01"}, {"type"=>"separator"}, {"type"=>"button", "action"=>{"type"=>"message", "label"=>"この商品です", "text"=>"この商品です"}}, {"type"=>"button", "action"=>{"type"=>"message", "label"=>"もっと見る", "text"=>"もっと見る"}}]}}]}
こうではなく、見たまんまの文字列で返したいです。
{
"type": "carousel",
"contents": [
{
"type": "bubble",
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "image",
"url": "https://xxx/xxxx.jpg"
},
{
"type": "text",
"text": "xxxx"
},
{
"type": "text",
"text": "2019-08-23"
},
{
"type": "separator"
},
{
"type": "button",
"action": {
"type": "message",
"label": "\u3053\u306e\u5546\u54c1\u3067\u3059",
"text": "\u3053\u306e\u5546\u54c1\u3067\u3059"
}
},
{
"type": "button",
"action": {
"type": "message",
"label": "\u3082\u3063\u3068\u898b\u308b",
"text": "\u3082\u3063\u3068\u898b\u308b"
}
}
]
}
}
★試したこと2
API追加して、carouselで受け取った値の中の
=>を:にstr_replaceさせてもダメでした。
{"type":"carousel", "contents":[
こう返ってきました。
補足情報(FW/ツールのバージョンなど)
$caroselsをJSONデコードして、それを文字列にすればよいのでしょうか?
意図としてLINEのチャットボットシステムでJSONを文字列で値を渡す処理があるのですが、
今のままだと間が=>となっているので、実現したいことが出来ずにおります。
★追記
print json_encode($arr, JSON_PRETTY_PRINT);
で吐き出されるこの部分
// これだけJSON形式にしない 文字列で返したい
$arr["carousel"] = $carosels;
これを取得した先で :が=>になります。
=>を手動で:に置き換えるとうまくJSON形式になり理想の動きになりました。
取得するときに=>が:になるようにするにはどうしたらよいでしょうか?
