Pouvez-vous faire un appel système? Vous pouvez utiliser un script python comme celui-ci pour convertir les données de conserves au vinaigre en 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)
De cette façon, si vous obtenir les données d'un fichier que vous pourriez faire quelque chose comme ceci:
<?php
exec("python pickle2json.py -p pickled_data.txt", $json_data = array());
?>
ou si vous voulez enregistrer dans un fichier ceci:
<?php
system("python pickle2json.py -p pickled_data.txt -j p_to_j.json");
?>
Tout le code ci-dessus est probablement pas parfait (je ne suis pas un développeur PHP), mais serait quelque chose comme ce travail pour vous?
Vous avez mentionné que la sérialisation n'est pas sous votre contrôle. Comment recevez-vous ces données décapées? Un fichier local? –
Il provient de la base de données de Django, c'est session_data dans la table de session django. Je finis probablement par écrire SessionMiddleware pour sérialiser la session_data en JSON. Je voulais juste ne pas avoir à modifier l'application Django pour cela. – Ciantic