<?PHPHeader("Content-type:text/html;charset=utf-8");include_once('./mysql.php ');$filename= "F:/heyu.txt";$handle=fopen($filename, "R");//when reading a binary file, you need to set the second parameter to ' RB '//To get the file size by filesize, and read the entire file into a string$contents=fread($handle,filesize($filename));$arr= Json_decode ($contents,true);$arr=$arr[' Weibosearchcontent '];$str _target= ' ';$str _data= ' ';$conndb=Newconndb ();foreach($arr as $i=$msg){ Echo $i.‘ <br> '; foreach($msg as $key=$val){ //echo $key. ' = '. $val. ' <br> '; $str _target.=$key.‘,‘; if($key= = ' UID '){ $val=Str_replace(' 1001030103 ', ',$val); } if($key= = ' titles ') {//traversing the moods array $tmp= ' '; foreach($val as $t=$v){ $tmp.=$v.‘,‘; } $tmp=RTrim($tmp,","); $str _data. = ' \ '.$tmp.‘ \‘,‘; } Else if($key= = ' pics ') {//traversing the moods array $tmp= ' '; foreach($val as $t=$v){ $tmp.=$v.‘,‘; } $tmp=RTrim($tmp,","); $str _data. = ' \ '.$tmp.‘ \‘,‘; } Else if($key= = ' moods ') {//traversing the moods array $tmp= ' '; foreach($val as $t=$v){ $tmp.=$v.‘,‘; } $tmp=RTrim($tmp,","); $str _data. = ' \ '.$tmp.‘ \‘,‘; } Else{ $str _data. = ' \ '.$val.‘ \‘,‘; } } //remove the last comma $str _target=RTrim($str _target, ","); $str _data=RTrim($str _data, ","); Echo $str _target.‘ <br> '; Echo $str _data.‘ <br> '; $sql= ' INSERT into Keyword_weibo ('.$str _target.‘) VALUES ('.$str _data.‘)‘; $conndb->query ($sql); $str _target= ' '; $str _data= ' '; }fclose($handle);?>
Parse JSON Insert Database