PHP将json返回给我:
{
"date" : "2014-02-06",
"typeDate": "local",
"events" : [
"To get know how Sherlock survived",
"Find who killed the President Kennedy",
"Cook met"
]
}
我需要将此对象插入到包含此类对象数组的表的字段中:
{
"dates": [
{
"date" : "2014-02-06",
"typeDate": "local",
"events" : [
"Kill John Connor",
"Kill T-1000",
"Listen Moon sonata"
]
},
{
"date" : "2014-02-07",
"typeDate": "local",
"events" : [
"Fix Enstain's theory",
"Take pizza"
]
}
]
}
如何做到这一点?试图使用示例
INSERT INTO aa VALUES (1, 'my_json_record');
我收到了关于json结构的错误。
答案 0 :(得分:1)
基本上你可以在插入数据库之前序列化JSON对象。
$my_json_var = json_decode($my_json_record)
这个methond采用JSON编码的字符串并将其转换为PHP变量。见http://us2.php.net/json_decode
然后构建您的查询......
$sql = "INSERT INTO aa (field1, field2) VALUES (1, '$my_json_var')";