#定义数组 $list = array( ["name" => "《西游记》", "author" => "吴承恩", "cat" => "A级书刊"], ["name" => "《三国演义》", "author" => "罗贯中", "cat" => "A级书刊"], ["name" => "《红楼梦》", "author" => "曹雪芹", "cat" => "A级书刊"] ); #转换为JSON数据格式,中文自动不转义; $data = json_encode($list, JSON_UNESCAPED_UNICODE); $content = "var data={$data};"; #写入文件; $fp = fopen("js/poidata.js", "w"); if ($fp != false) { fwrite($fp, $content, strlen($content)); fclose($fp); } echo "OK";
python版
# -*- coding: UTF-8 -*- # @Time : 2021-09-08 13:52 # @File : makejs.py # @Author : LockDataV list = [{"name": "宁波市鄞州区泰康中路558号"}, {"name": "苏州市常熟市常福街道海虞北路288号"}] # print(list) content = 'var data ={}'.format(list); # 写入文件 with open('data.js', 'w', encoding="utf-8") as f: f.write(content)
lockdatav Done!