编程语言
首页 > 编程语言> > python 正则表达式与JSON-JSON

python 正则表达式与JSON-JSON

作者:互联网

1、json 是一种轻量级的跨语言数据交换格式。

轻量的意思是 比起xml来说,更加轻量。比xml传输的数据更易读,更易于极细,传输的效率要高。

2、怎么检查,一个字符串是个json字符串?

json 的key 和字符串value 都是用双引号扩起来 不能是单引号

能解析:解析成dict,读取时按照dict的存取方式读就好了

import json
#coding=utf-8
json_str = '{"name":"anson","age":18}'
student = json.loads(json_str)
print(student)
# [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test15.py"
# {u'age': 18, u'name': u'anson'}

不能解析:

json_str = '{'name':'anson','age':18}'
[Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test15.py"
  File "/Users/anson/Documents/Project/python_ToolCodes/test15.py", line 4
    json_str = '{'name':'anson','age':18}'
                     ^
SyntaxError: invalid syntax

不能解析:

json_str = "{'name':'anson','age':18}"
[Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test15.py"
Traceback (most recent call last):
  File "/Users/anson/Documents/Project/python_ToolCodes/test15.py", line 5, in <module>
    student = json.loads(json_str)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

3、理解下半句:json时一种数据交换格式。数据格式有很多,比如字符串、数组

json_str = '{"name":"anson","age":18}'

被解析后,解析出来是这样

{u'age': 18, u'name': u'anson'}

是个dict类型

import json
#coding=utf-8
json_str = '{"name":"anson","age":18}'
student = json.loads(json_str)
print(type(student))
# [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test15.py"
# <type 'dict'>

 

 

json_arr = '[{"name":"anson","age":18},{"name":"kiki","age":19}]'

这么解析能成功:

import json
#coding=utf-8
#json_str = '{"name":"anson","age":18}'
json_arr = '[{"name":"anson","age":18},{"name":"kiki","age":19}]'
student = json.loads(json_arr)
print(student)
# [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test15.py"
# [{u'age': 18, u'name': u'anson'}, {u'age': 19, u'name': u'kiki'}]
import json
#coding=utf-8
#json_str = '{"name":"anson","age":18}'
json_arr = '[{"name":"anson","age":18},{"name":"kiki","age":19}]'
student = json.loads(json_arr)
print(type(student))
# [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test15.py"
# <type 'list'>

解析出来是个list类型

 

总结:json 格式于python 解析格式一览表

json        python

object      dict

array       list

string      dict

number      int

number      float

true        True

false       False

null        None

 

3、json 序列化:python的数据类型格式转成json

#coding=utf-8
import json
student = [
    {'name':'anson','age':18,'flag':False},
    {'name':'kiki', 'age':19,'flag':True},
]

json_str = json.dumps(student)
print(type(json_str))
print (json_str)
# [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test16.py"
# <type 'str'>
# [{"age": 18, "flag": false, "name": "anson"}, {"age": 19, "flag": true, "name": "kiki"}]

4、总结一个误区

JSON 对象 JSON 和JSON 字符串是啥,有啥区别?

JSON 对象:JSON 对象放到Javascript里面,是有这个概念的,但是放到python里面,就没这种说法了(大神的理解,我就是死记硬背)

JSON:是个概念,说的是一种数据交换格式

JSON 字符串:不说了 一直讲的都是json字符串

标签:name,python,18,age,json,anson,正则表达式,JSON
来源: https://www.cnblogs.com/ansonwan/p/13448079.html