Analytic Json
一、json反序列化
# -*- coding:utf-8 -*-
# 载入json库
import json
## 读取json文件
## dbconfig_file变量为文件
dbconfig_file = open("dbConfig.json")
dbconfig_json = json.load(dbconfig_file)
dbconfig_json
{u'db_info': {u'mongodb_dbName': u'zhixun',
u'mongodb_host': u'10.31.129.131',
u'mongodb_password': u'QBZi8oDfSPuwl89W',
u'mongodb_port': u'27017',
u'mongodb_user': u'zhixun_test',
u'mysql_dbName': u'zhixun_fws',
u'mysql_host': u'rm-bp11357l9jkx20z8o.mysql.rds.aliyuncs.com',
u'mysql_password': u'iZtBC2XDCCWscAoFan3',
u'mysql_port': u'3306',
u'mysql_user': u'zx_user0001',
u'test_parse': u'{{key:test_parse}}',
u'version': u'0.1.0'}}
文件类型为字典,可以通过[key]取出对应的value
print dbconfig_json['db_info']['mongodb_dbName']
type(dbconfig_json)
zhixun
dict
二、json序列化
字典变量序列化为json
print json.dumps.__doc__
Serialize ``obj`` to a JSON formatted ``str``.
If ``skipkeys`` is true then ``dict`` keys that are not basic types
(``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
will be skipped instead of raising a ``TypeError``.
If ``ensure_ascii`` is false, all non-ASCII characters are not escaped, and
the return value may be a ``unicode`` instance. See ``dump`` for details.
If ``check_circular`` is false, then the circular reference check
for container types will be skipped and a circular reference will
result in an ``OverflowError`` (or worse).
If ``allow_nan`` is false, then it will be a ``ValueError`` to
serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
strict compliance of the JSON specification, instead of using the
JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
If ``indent`` is a non-negative integer, then JSON array elements and
object members will be pretty-printed with that indent level. An indent
level of 0 will only insert newlines. ``None`` is the most compact
representation. Since the default item separator is ``', '``, the
output might include trailing whitespace when ``indent`` is specified.
You can use ``separators=(',', ': ')`` to avoid this.
If ``separators`` is an ``(item_separator, dict_separator)`` tuple
then it will be used instead of the default ``(', ', ': ')`` separators.
``(',', ':')`` is the most compact JSON representation.
``encoding`` is the character encoding for str instances, default is UTF-8.
``default(obj)`` is a function that should return a serializable version
of obj or raise TypeError. The default simply raises TypeError.
If *sort_keys* is ``True`` (default: ``False``), then the output of
dictionaries will be sorted by key.
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
``.default()`` method to serialize additional types), specify it with
the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
## 定义一个字典变量
mydict = {'db_info': {'mongodb_dbName': 'zhixun',
'mongodb_host': '10.31.129.131',
'mongodb_password': 'QBZi8oDfSPuwl89W',
'mongodb_port': '27017',
'mongodb_user': 'zhixun_test',
'mysql_dbName': 'zhixun_fws',
'mysql_host': 'rm-bp11357l9jkx20z8o.mysql.rds.aliyuncs.com',
'mysql_password': 'iZtBC2XDCCWscAoFan3',
'mysql_port': '3306',
'mysql_user': 'zx_user0001',
'test_parse': '{{key:test_parse}}',
'version': '0.1.0'}}
type(mydict)
dict
## json.dumps序列化函数,字典转化为json字符串
myjson = json.dumps(mydict)
print myjson
{"db_info": {"mongodb_port": "27017", "mongodb_dbName": "zhixun", "mongodb_password": "QBZi8oDfSPuwl89W", "mysql_password": "iZtBC2XDCCWscAoFan3", "mysql_user": "zx_user0001", "mysql_dbName": "zhixun_fws", "mysql_host": "rm-bp11357l9jkx20z8o.mysql.rds.aliyuncs.com", "test_parse": "{{key:test_parse}}", "version": "0.1.0", "mysql_port": "3306", "mongodb_user": "zhixun_test", "mongodb_host": "10.31.129.131"}}
## 变量类型字符串
type(myjson)
str