json对象的值赋给php数组,然后数组的健值,存到mysql内对应字段名下。


json对象值赋给php变量的数组健名,存到mysql内对应字段下。
应该怎么做 ?


 CREATE TABLE IF NOT EXISTS `pm2_5-json` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `aqi` int(10) NOT NULL,
  `area` varchar(255) NOT NULL,
  `pm2_5` int(10) NOT NULL,
  `pm2_5_24h` int(10) NOT NULL,
  `position_name` varchar(255) NOT NULL,
  `primary_pollutant` varchar(255) NOT NULL,
  `quality` varchar(255) NOT NULL,
  `station_code` varchar(255) NOT NULL,
  `time_point` varchar(255) NOT NULL,
  `create_time` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

搞了一个早上没有找到思路,给我一段代码吧,谢谢了!
用的是php的CI框架。


 [{"aqi":32,"area":"珠海","pm2_5":22,"pm2_5_24h":24,"position_name":"吉大","primary_pollutant":null,"quality":"优","station_code":"1367A","time_point":"2014-03-07T07:00:00Z"},{"aqi":32,"area":"珠海","pm2_5":22,"pm2_5_24h":28,"position_name":"前山","primary_pollutant":null,"quality":"优","station_code":"1368A","time_point":"2014-03-07T07:00:00Z"},{"aqi":36,"area":"珠海","pm2_5":25,"pm2_5_24h":32,"position_name":"唐家","primary_pollutant":null,"quality":"优","station_code":"1369A","time_point":"2014-03-07T07:00:00Z"},{"aqi":32,"area":"珠海","pm2_5":22,"pm2_5_24h":28,"position_name":"斗门","primary_pollutant":null,"quality":"优","station_code":"1370A","time_point":"2014-03-07T07:00:00Z"},{"aqi":31,"area":"珠海","pm2_5":22,"pm2_5_24h":28,"position_name":null,"primary_pollutant":"","quality":"优","station_code":null,"time_point":"2014-03-07T07:00:00Z"}]

json jsonp

QなDT酱 11 years ago

前端代码:


 <form action='api.php' method="post">
    <input name='json_data' type='text' value='[{"a11":"v11","a12":"v12"},{"a21":"v21","a22":"v22"}]' />
    <input type="submit" value="submitAction" />
</form>

后端代码:


 <?php
header("content-type:text/html;charset=utf-8");
echo "<pre>";
print_r(json_decode($_POST['json_data']));
echo "</pre>";

兔子sky answered 11 years ago

Your Answer