你能做一个系统调用吗?您可以使用这样的 python 脚本将 pickle 数据转换为 json:
# pickle2json.py
import sys, optparse, cPickle, os
try:
import json
except:
import simplejson as json
# Setup the arguments this script can accept from the command line
parser = optparse.OptionParser()
parser.add_option('-p','--pickled_data_path',dest="pickled_data_path",type="string",help="Path to the file containing pickled data.")
parser.add_option('-j','--json_data_path',dest="json_data_path",type="string",help="Path to where the json data should be saved.")
opts,args=parser.parse_args()
# Load in the pickled data from either a file or the standard input stream
if opts.pickled_data_path:
unpickled_data = cPickle.loads(open(opts.pickled_data_path).read())
else:
unpickled_data = cPickle.loads(sys.stdin.read())
# Output the json version of the data either to another file or to the standard output
if opts.json_data_path:
open(opts.json_data_path, 'w').write(json.dumps(unpickled_data))
else:
print json.dumps(unpickled_data)
这样,如果您从文件中获取数据,您可以执行以下操作:
<?php
exec("python pickle2json.py -p pickled_data.txt", $json_data = array());
?>
或者如果你想把它保存到一个文件中:
<?php
system("python pickle2json.py -p pickled_data.txt -j p_to_j.json");
?>
上面的所有代码可能都不完美(我不是 PHP 开发人员),但是这样的代码对你有用吗?