1、常用json库主要有json-py和simplejson
1) json-py 包含json和minjson,用法一样
Python 2.4. (#, Jan , ::)
[GCC 4.1. (Red Hat 4.1.-)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> import minjson
>>> a = {'a':, 'b':}
>>> b = json.write(a)
>>> c = json.read(b)
>>> a
{'a': , 'b': }
>>> b
'{"a":5,"b":4}'
>>> c
{'a': , 'b': }
>>> d = minjson.write(a)
>>> e = minjson.read(d)
>>> d
'{"a": 5, "b": 4}'
>>> e
{'a': , 'b': }
>>>
2) simplejson
# python .6以上:
import json
json.loads(js_obj)
json.dumps(py_obj) # python .6以下:
import simplejson as json
json.loads(js_obj)
json.dumps(py_obj)
2、安装simplejson
python2.6以上内置json解释库,是 smiplejson,python2.6以下没有内置的json,要手动安装。下载地址是 https://pypi.python.org/packages/source/s/simplejson/simplejson-2.0.9.tar.gz。如果下载太慢可使用pypi豆瓣源
[root@LuGu_10_1_80_198 py]# cd simplejson-2.0.
[root@LuGu_10_1_80_198 simplejson-2.0.]# /usr/bin/python2. setup.py install [root@LuGu_10_1_80_198 simplejson-2.0.]# /usr/bin/python2.
Python 2.4. (#, Jan , ::)
[GCC 4.1. (Red Hat 4.1.-)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import simplejson as json
>>> a = {'a': , 'b': }
>>> b = json.dumps(a)
>>> b
'{"a": 5, "b": 4}'
>>> c = json.loads(b)
>>> c
{u'a': , u'b': }
>>> type(c)
<type 'dict'>
>>> type(b)
<type 'str'>
>>>
3、安装脚本 install_simplejson.sh
#!/bin/bash cat >/usr/local/src/test.py <<EOF
#!/usr/bin/python
import sys try:
import json
except ImportError:
try:
import simplejson as json
except ImportError:
sys.exit()
sys.exit()
EOF /usr/bin/python /usr/local/src/test.py &>/dev/null
if [ $? -eq ];then
echo "json or simplejson have installed"
exit
fi # json="json-py-3_4.zip" # python .6之前需要安装simplejson .6和之后内置json
simplejson="simplejson-2.0.9.tar.gz"
setuptool="setuptools-0.6c7-py2.4.egg" rsync -arv rsync://root@10.1.17.217/upload/simplejson-forpy /usr/local/src &>/dev/null cd /usr/local/src/simplejson-forpy tar -xf ${simplejson} && softwaredir=`echo ${simplejson} | sed 's/\.tar.gz//g'`
mv ${setuptool} ${softwaredir} && cd ${softwaredir}
/usr/bin/python setup.py install >/dev/null >&
cd /usr/local/src && rm -rf simplejson-forpy /usr/bin/python /usr/local/src/test.py &>/dev/null
if [ $? -eq ];then
rm -rf /usr/local/src/test.py
echo "Python simplejson Install OK" && exit
else
rm -rf /usr/local/src/test.py
echo "python simplejson Install OK" && exit
fi
原创文章,转载请备注原文地址 http://www.cnblogs.com/lxmhhy/p/6029556.html
知识交流讨论请加qq群:180214441。谢谢合作