将JSON代码附加到从MySQL和PHP创建的JSON
我正在尝试从MySQL动态创建JSON,使用我在这个线程中找到的这段代码似乎相当容易。 但是,我想在$json_响应前后添加一些json代码 主代码将JSON代码附加到从MySQL和PHP创建的JSON,php,mysql,json,Php,Mysql,Json,我正在尝试从MySQL动态创建JSON,使用我在这个线程中找到的这段代码似乎相当容易。 但是,我想在$json_响应前后添加一些json代码 主代码 $result = mysql_query("SELECT * FROM Places "); $json_response = array(); //Create an array while ($row = mysql_fetch_array($result)) { $row_array = array(); $row
$result = mysql_query("SELECT * FROM Places ");
$json_response = array(); //Create an array
while ($row = mysql_fetch_array($result))
{
$row_array = array();
$row_array['title'] = $row['title'];
$row_array['image_url'] = $row['image_url'];
$row_array['subtitle'] = $row['subtitle'];
$row_array['buttons'] = array();
$id = $row['id'];
$option_qry = mysql_query("SELECT * FROM Places where id=$id");
while ($opt_fet = mysql_fetch_array($option_qry))
{
$row_array['buttons'][] = array(
'type' => $opt_fet['type'],
'caption' => $opt_fet['caption'],
'url' => $opt_fet['url'],
);
}
array_push($json_response, $row_array); //push the values in the array
}
echo json_encode($json_response, JSON_PRETTY_PRINT);
生成此JSON
[
{
"title": "Name of the place",
"image_url": "image.jpg",
"subtitle":Additional info",
"buttons": [
{
"type": "'url'",
"caption": "More Info",
"url": "https://some link "
}
]
},
{
"title": "Name of the place 2",
"image_url": "image2.jpg",
"subtitle":Additional info2",
"buttons": [
{
"type": "'url'",
"caption": "More Info",
"url": "https://some link 2"
}
]
}
]
{
"version": "v2",
"content": {
"messages": [
{
"type": "cards",
"elements":
我不得不在已经创建的JSON之前添加以下代码
[
{
"title": "Name of the place",
"image_url": "image.jpg",
"subtitle":Additional info",
"buttons": [
{
"type": "'url'",
"caption": "More Info",
"url": "https://some link "
}
]
},
{
"title": "Name of the place 2",
"image_url": "image2.jpg",
"subtitle":Additional info2",
"buttons": [
{
"type": "'url'",
"caption": "More Info",
"url": "https://some link 2"
}
]
}
]
{
"version": "v2",
"content": {
"messages": [
{
"type": "cards",
"elements":
最后是这段代码
}
]
}
}
非常简单:
$final_json = [
"version" => "v2",
"content" => [
"messages" => [[
"type" => "cards",
"elements" => $json_response
]]
]
];
echo json_encode($final_json, JSON_PRETTY_PRINT);
就个人而言,为了清晰起见,我将$json\u response
重命名为$messages
。非常简单:
$final_json = [
"version" => "v2",
"content" => [
"messages" => [[
"type" => "cards",
"elements" => $json_response
]]
]
];
echo json_encode($final_json, JSON_PRETTY_PRINT);
就个人而言,为了清晰起见,我将
$json_response
重命名为$messages
。同时声明array$json_response=array()代码>你可以用你的默认值来准备它
$stdObj=new \stdClass();
$stdObj->version="V2";
$stdObj->content=(object)["messages"=>(object)["type"=>'cards','elements'=>$row_array['buttons']]];
echo json_encode($stdObj, JSON_PRETTY_PRINT);
声明数组时$json_response=array()代码>你可以用你的默认值来准备它
$stdObj=new \stdClass();
$stdObj->version="V2";
$stdObj->content=(object)["messages"=>(object)["type"=>'cards','elements'=>$row_array['buttons']]];
echo json_encode($stdObj, JSON_PRETTY_PRINT);
很酷,最后添加代码会怎么样?或者它自动添加它是我写评论后注意到的:)它抛出了一个错误——语法错误,意外的“:”,期望的“]”——这在“版本”行上:“:”v2“,@lStoilov抱歉,修复了(PHP中的数组语法是=>
而不是:
)。@FrankerZ,谢谢。。。我刚刚注意到{“type”:“cards”,“elements”:缺失:)你能帮我添加它吗?很酷,最后添加代码会怎么样?或者它会自动添加?是的,我在写了我的评论后注意到:)它抛出了一个错误——语法错误,意外的“:”,预期的“]---在“version”:“v2”行上,@lStoilov抱歉,已修复(PHP中的数组语法是=>
而不是:
)。@FrankerZ,谢谢……我刚刚注意到{“type”:“cards”,“elements”:缺少:)您可以帮助添加它吗。谢谢,第三行中的某个地方有一个错误-它说:语法错误,意外的“;”,期望“]'更新了答案]丢失了为什么要将所有内容从数组转换回对象?我们可以按照您指定的方法获得@FrankerZ的结果,但这也是一种可能的方法。谢谢s、 第三行中的某个地方有一个错误-它说:语法错误,意外的“;”,期望“]'更新了答案]丢失了为什么要将所有内容从数组转换回对象?我们可以通过指定的方法获得@FrankerZ的结果,但这也是一种可能的方法。