Bạn có thể thực hiện cuộc gọi hệ thống không? Bạn có thể sử dụng một kịch bản python như thế này để chuyển đổi dữ liệu dưa vào 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)
Bằng cách này, nếu bạn nhận được dữ liệu từ một tập tin bạn có thể làm một cái gì đó như thế này:
<?php
exec("python pickle2json.py -p pickled_data.txt", $json_data = array());
?>
hoặc nếu bạn muốn lưu tệp này vào một tệp này:
<?php
system("python pickle2json.py -p pickled_data.txt -j p_to_j.json");
?>
Tất cả mã ở trên có lẽ không hoàn hảo (tôi không phải là nhà phát triển PHP), nhưng điều này có thể phù hợp với bạn?
Nguồn
2010-06-14 21:56:11
Bạn đã đề cập đến việc tuần tự hóa không nằm trong tầm kiểm soát của bạn. Làm thế nào bạn nhận được dữ liệu ngâm này? Tệp cục bộ? –
Nó đến từ cơ sở dữ liệu ở Django, đó là session_data trong bảng phiên django. Tôi có thể sẽ thực sự viết SessionMiddleware để tuần tự hóa session_data như JSON. Tôi chỉ ước tôi không phải sửa đổi ứng dụng Django cho việc này. – Ciantic