准备工作

import json

# 准备数据:
d = dict(name = 'Tom',age = 18)
json_str = '{"name":"Tom","age":18}'
# 注:json字符串中的引号必须为双引号,若为单引号会转换出错。

json数据类型和python数据类型的对应关系

  • {} <——> dict
  • [] <——> list
  • "string" <——> "str"或u"unicode"
  • 123.4 <——> int或float
  • true/false <——> True/False
  • null <——> None

常用方法

把字典转换成json字符串

ret = json.dumps(d)
print ret
print type(ret)
{"age": 18, "name": "Tom"}
<type 'str'>

把json字符串转成字典

ret = json.loads(json_str)
print ret
print type(ret)
{u'age': 18, u'name': u'Tom'}
<type 'dict'>

把字典转换成json字符串并写入文件

with open('out.txt','w+') as f:
json.dump(d,f)

从文件中读取一个json字符串并转换为字典

# 文件(out.txt)内容:{"age": 18, "name": "Tom"}
with open('out.txt','r') as f:
ret = json.load(f)
print ret
print type(ret)
{u'age': 18, u'name': u'Tom'}
<type 'dict'>

自定义对象转成json字符串

class Student(object):
def __init__(self,name,age):
self.name = name
self.age = age s = Student('Tom',18)
print json.dumps(s)
# 输出:
# TypeError: <__main__.Student object at 0x7f7ab808cf10> is not JSON serializable

出错原因:Student对象不是一个可序列化为json的对象。

  • 解决方法1:写个转换函数
def student2dict(std):
return {'name':std.name,'age':std.age}
print json.dumps(s,default = student2dict)
{"age": 18, "name": "Tom"}
  • 解决方法2:传入Student对象内置属性:dict
print json.dumps(s,default = lambda obj:obj.__dict__)
{"age": 18, "name": "Tom"}

json字符串转换为自定义对象

def dict2student(d):
return Student(d['name'],d['age'])
ret = json.loads(json_str,object_hook = dict2student)
print ret
print ret.__dict__
print type(ret)
<__main__.Student object at 0x7f7aaa713ad0>
{'age': 18, 'name': u'Tom'}
<class '__main__.Student'>

补充

更好地输出json

json.dumps(json.loads(json_str),indent = 4)  # indent为缩进的字符数
'{\n    "age": 18, \n    "name": "Tom"\n}'

保持json字符串中属性的顺序

from collections import OrderedDict
data = json.loads(json_str,object_pairs_hook = OrderedDict)
print data
OrderedDict([(u'name', u'Tom'), (u'age', 18)])

引申:object_pairs_hook是个什么玩意?

这时候就有疑惑了,这个object_pairs_hoo参数是个什么玩意?为什么加上:object_pairs_hook = OrderedDict这样一个参数,解析的字典就可以有序了?

为了揭开这个谜团,首先去看看json.loads()函数文档,发现文档中对object_pairs_hook参数的描述是这样的:

``object_pairs_hook`` is an optional function that will be called with the
result of any object literal decoded with an ordered list of pairs. The
return value of ``object_pairs_hook`` will be used instead of the ``dict``.
This feature can be used to implement custom decoders that rely on the
order that the key and value pairs are decoded (for example,
collections.OrderedDict will remember the order of insertion). If
``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.

大致意思就是:object_pairs_hook实际上是一个函数对象(钩子函数),它的入参是json文本的有序键值对的列表(ordered list of pairs),返回值是一个经过自定义处理的值,json.loads()函数的返回值也会是这个钩子函数的返回值。

说了半天估计也没看明白,那就实际写个demo试一把,先看最简单的一个demo:

# coding:utf-8
import json def deal_with_pairs(pairs):
'''
自定义的钩子函数,处理从json文本中解析出的有序键值对列表
:param pairs: 从json文本中解析出的有序键值对列表
:return: 自定义的对象
'''
return pairs json_str = '{"a":"111","b":"222"}'
data = json.loads(json_str,object_pairs_hook = deal_with_pairs)
print data

输出:

[(u'a', u'111'), (u'b', u'222')]

可以看出,输出的就是json文本中的有序键值对列表。

下面继续看一个稍微复杂一点的demo:

# coding:utf-8
import json # 存放json中重复的key列表
duplicate_keys = [] def deal_with_pairs(pairs):
'''
自定义的钩子函数,处理从json文本中解析出的有序键值对列表
:param pairs: 从json文本中解析出的有序键值对列表
:return: 自定义的对象
'''
data = {}
for k,v in pairs:
# 如果键已经在data的键中存在了,那么把它添加到duplicate_keys列表
if k in data:
duplicate_keys.append(k)
# 否则添加到data中
else:
data[k] = v return data json_str = '{"a":"111","b":"222","a":"345"}'
data = json.loads(json_str,object_pairs_hook = deal_with_pairs)
print data
print duplicate_keys

输出:

{u'a': u'111', u'b': u'222'}
[u'a']

可以看出,上面这个程序的作用就是找出了json文本中有哪些键是重复的。

最后再来一个嵌套的有重复key的json字符串,来看看效果:

# coding:utf-8
import json # 存放json中重复的key列表
duplicate_keys = [] def deal_with_pairs(pairs):
'''
自定义的钩子函数,处理从json文本中解析出的有序键值对列表
:param pairs: 从json文本中解析出的有序键值对列表
:return: 自定义的对象
'''
print 'pairs is: {0}'.format(pairs)
data = {}
for k,v in pairs:
# 如果键已经在data的键中存在了,那么把它添加到duplicate_keys列表
if k in data:
duplicate_keys.append(k)
# 否则添加到data中
else:
data[k] = v return data json_str = '{"a":"111","b":{"b1":"b111","b2":"b222","b1":"b123"},"a":"345"}'
data = json.loads(json_str,object_pairs_hook = deal_with_pairs)
print data
print duplicate_keys

输出:

pairs is: [(u'b1', u'b111'), (u'b2', u'b222'), (u'b1', u'b123')]
pairs is: [(u'a', u'111'), (u'b', {u'b1': u'b111', u'b2': u'b222'}), (u'a', u'345')]
{u'a': u'111', u'b': {u'b1': u'b111', u'b2': u'b222'}}
[u'b1', u'a']

可以看出这里输出了两个pairs列表,第一个是内层的子json的键值对列表,第二个是外层的json键值对列表。最终查找出来的重复的键有:'b1'和'a',和我们的预期相符。

随机推荐

  1. Petrozavodsk Summer-2015. Ivan Smirnov Contest 1 B Bloom

    http://opentrains.snarknews.info/~ejudge/team.cgi?contest_id=001463 题目大意:给出$n$个$x$,$m$个$y$,问有多少个hash ...

  2. 开启GitHub模式,now!

    (原文地址为:http://www.karottc.com/blog/2014/06/15/current-doing/) 最近看到了一篇文章,该文章的作者将自己连续177天在github上commi ...

  3. centos7 禁用每次sudo 需要输入密码

    安装完centos7后,默认没有启用sudo,首先应该是对sudo进行设置.sudo的作用就是使当前非root用户在使用没有权限的命令 时,直接在命令前加入sudo,在输入自己当前用户的密码就可以完成 ...

  4. linux和android端的pthread学习

    本文起初主要想写个演示样例实測下pthread_mutex_lock和pthread_mutex_trylock差别.在linux机器上非常快就over了,可是想了一下.pthread是unix系的, ...

  5. Learning to Compare: Relation Network 源码调试

    CVPR 2018 的一篇少样本学习论文 Learning to Compare: Relation Network for Few-Shot Learning 源码地址:https://github ...

  6. C++ TR1、TR2与boost的关系

    C++ Technical Report 1 (TR1)是ISO/IEC TR 19768, C++ Library Extensions(函式库扩充)的一般名称.TR1是一份文件,内容提出了对C++ ...

  7. const在指针中的用法

    一.指向const对象的指针---对象不能修改 方式1 int value1 = 3; const int *p1 = &value1; *p1 = 5; //错误,不能修改const指向对象 ...

  8. Android 调用堆栈跟踪

    Android开发中,我们也会经常遇到段错误,也就是SIGSEGV(11),这个时候libc的backtrace会打印出对应的堆栈信 息,而你看到的仅仅是一对数字,好像无从查起. 如下面这一从串断错误 ...

  9. win10下Import caffe时出现“ImportError: No module named google.protobuf.internal”的解决办法

    解决方法:只要出现和protobuf相关的错误,只要在cmd中输入pip install protobuf,然后等待安装完成即可. ps:这时,可能会出现"pip 不是内部命令"之 ...

  10. finereport-JS

    JS实现定时刷新报表 setInterval("self.location.reload();",10000); //10000ms即每10s刷新一次页面. 注:对于cpt报表,若 ...