[英]Insert json into array of table's field in Postgres
PHP returns me the json: PHP向我返回json:
{
"date" : "2014-02-06",
"typeDate": "local",
"events" : [
"To get know how Sherlock survived",
"Find who killed the President Kennedy",
"Cook met"
]
}
I need to insert this object into table's field which has the array of such objects: 我需要将此对象插入具有此类对象数组的表字段中:
{
"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"
]
}
]
}
How to do this correct ? 如何做到这一点正确? Trying to use the example
尝试使用示例
INSERT INTO aa VALUES (1, 'my_json_record');
I'm getting the error about structure of json. 我收到有关json结构的错误。
hbasically you soould serialize the JSON object before inserting into the database. 通常,您应该先序列化JSON对象,然后再插入数据库。
$my_json_var = json_decode($my_json_record)
This methond takes a JSON encoded string and converts it into a PHP variable. 此方法使用JSON编码的字符串并将其转换为PHP变量。 see http://us2.php.net/json_decode
参见http://us2.php.net/json_decode
Then build your query... 然后建立您的查询...
$sql = "INSERT INTO aa (field1, field2) VALUES (1, '$my_json_var')";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.