使用Python编码、解码JSON对象
2018-02-27 20:37
411 查看
测试版本为Python2.7import json
pObj = {
"int":10,
"string":"test",
"true":True,
"false":False,
"null":None,
"list":["a"],
"tuple":("a",)
}
print "type=%s content=%s"%(type(pObj), pObj)
jsonStr = json.dumps(pObj) #编码
print "type=%s content=%s"%(type(jsonStr), jsonStr)
obj= json.loads(jsonStr) #解码
print "type=%s content=%s"%(type(obj), obj)执行结果如下:type=<type 'dict'> content={'false': False, 'string': 'test', 'tuple': ('a',), 'int': 10, 'list': ['a'], 'null': None, 'true': True}
type=<type 'str'> content={"false": false, "string": "test", "tuple": ["a"], "int": 10, "list": ["a"], "null": null, "true": true}
type=<type 'dict'> content={u'false': False, u'string': u'test', u'tuple': [u'a'], u'int': 10, u'list': [u'a'], u'null': None, u'true': True}参考Python源码中的注释,编码结果如下图+-------------------+---------------+
| Python | JSON |
+===================+===============+
| dict | object |
+-------------------+---------------+
| list, tuple | array |
+-------------------+---------------+
| str, unicode | string |
+-------------------+---------------+
| int, long, float | number |
+-------------------+---------------+
| True | true |
+-------------------+---------------+
| False | false |
+-------------------+---------------+
| None | null |
+-------------------+---------------+解码结果如下图:+---------------+-------------------+
| JSON | Python |
+===============+===================+
| object | dict |
+---------------+-------------------+
| array | list |
+---------------+-------------------+
| string | unicode |
+---------------+-------------------+
| number (int) | int, long |
+---------------+-------------------+
| number (real) | float |
+---------------+-------------------+
| true | True |
+---------------+-------------------+
| false | False |
+---------------+-------------------+
| null | None |
+---------------+-------------------+在解码的过程中遇到了一些问题:import json
a = '{"name":"sean"}'
#b = '{'''name''':'''sean'''}' 编译异常
c = "{'name':'sean'}"
d = "{'''name''':'''sean'''}"
e = '''{'name':'sean'}'''
f = '''{"name":"sean"}'''
g = "{\"name\":\"sean\"}"
obj = json.loads(g)
print "type=%s content=%s" % (type(obj), obj)debug结果如图
无论最外层使用单引号、双引号还是三引号,解析器解析后都会变为单引号,只要内外层引号不重复即可,否则内层引号前将添加\\导致解析错误
pObj = {
"int":10,
"string":"test",
"true":True,
"false":False,
"null":None,
"list":["a"],
"tuple":("a",)
}
print "type=%s content=%s"%(type(pObj), pObj)
jsonStr = json.dumps(pObj) #编码
print "type=%s content=%s"%(type(jsonStr), jsonStr)
obj= json.loads(jsonStr) #解码
print "type=%s content=%s"%(type(obj), obj)执行结果如下:type=<type 'dict'> content={'false': False, 'string': 'test', 'tuple': ('a',), 'int': 10, 'list': ['a'], 'null': None, 'true': True}
type=<type 'str'> content={"false": false, "string": "test", "tuple": ["a"], "int": 10, "list": ["a"], "null": null, "true": true}
type=<type 'dict'> content={u'false': False, u'string': u'test', u'tuple': [u'a'], u'int': 10, u'list': [u'a'], u'null': None, u'true': True}参考Python源码中的注释,编码结果如下图+-------------------+---------------+
| Python | JSON |
+===================+===============+
| dict | object |
+-------------------+---------------+
| list, tuple | array |
+-------------------+---------------+
| str, unicode | string |
+-------------------+---------------+
| int, long, float | number |
+-------------------+---------------+
| True | true |
+-------------------+---------------+
| False | false |
+-------------------+---------------+
| None | null |
+-------------------+---------------+解码结果如下图:+---------------+-------------------+
| JSON | Python |
+===============+===================+
| object | dict |
+---------------+-------------------+
| array | list |
+---------------+-------------------+
| string | unicode |
+---------------+-------------------+
| number (int) | int, long |
+---------------+-------------------+
| number (real) | float |
+---------------+-------------------+
| true | True |
+---------------+-------------------+
| false | False |
+---------------+-------------------+
| null | None |
+---------------+-------------------+在解码的过程中遇到了一些问题:import json
a = '{"name":"sean"}'
#b = '{'''name''':'''sean'''}' 编译异常
c = "{'name':'sean'}"
d = "{'''name''':'''sean'''}"
e = '''{'name':'sean'}'''
f = '''{"name":"sean"}'''
g = "{\"name\":\"sean\"}"
obj = json.loads(g)
print "type=%s content=%s" % (type(obj), obj)debug结果如图
无论最外层使用单引号、双引号还是三引号,解析器解析后都会变为单引号,只要内外层引号不重复即可,否则内层引号前将添加\\导致解析错误
相关文章推荐
- Python中json格式数据的编码和解码
- ExtJs4之Ext.util.JSON编码和解码JSON对象
- Python中json格式数据的编码与解码方法详解
- php编码解码json对象
- Python基础-Json编码和解码操作
- EXTJS学习系列基础篇:第四篇(转载)作者殷良胜,Ext中有两个很重要的方法,一个是decode;一个是encode.顾名思义,一个是编码,一个是解码,你难道真的这么想吗?严格的说,一个是将json字符串转换成对象;一个是将对象转换成json字符串
- mina仿qq聊天功能,自定义协议,协议的编码和解码详解,发送xml对象json,mina开发大全,详细api,mina心跳
- Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 1 (char 1)
- [解决办法]Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 2 (char 1)
- Python使用email模块对邮件进行编码和解码的实例教程
- python菜鸟笔记--python关于json对象的使用方法学习
- Python中json.loads的时候出错->要注意要解码的Json字符的编码
- [解决办法]Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 2 (char 1)
- Python中json格式数据的编码和解码
- [解决办法]Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 2 (char 1)
- Golang 奇葩的时间包之Json与struct对象之间互相转换,JsonDate和JsonTime自定义类型如何实现json编码和解码(二)
- python中json.dumps使用及字符编码
- python requests返回的json对象用json.loads()时转为字典时编码变为了unicode
- 【原】使用Json作为Python和C#混合编程时对象转换的中间文件
- Python使用email模块对邮件进行编码和解码的实例教程