python模块之JSON

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#python模块之JSON

#1、JSON
#JSON表示的对象就是标准的JavaScript语言的对象



#2、json数据类型和python数据类型对应关系为:
'''
json类型            python类型
  {}                  dict
  []                  list
  string              str
  123.56              int/float
  true/false          True/False
  null                 None
'''


#3、如何将python对象转化为json格式--dumps
import json
d=dict(name='xiaodeng',age=28,score=100)
print (type(d))

result=json.dumps(d)
#print result#{"age": 28, "name": "xiaodeng", "score": 100}
print (type(result))##<type 'str'>



#4、如何将json格式转化为python对象--loads
json_str = '{"age": 20, "score": 88, "name": "Bob"}'
string=json.loads(json_str)
print (string)#{'name': 'Bob', 'score': 88, 'age': 20}
print (type(string))#



#5、案例
import json
string='{"name":"test", "type":{"name":"seq", "parameter":["1", "2"]}}'
s = json.loads(string)
print (1,type(s))#
print (s.keys())#[u'type', u'name']
print (s['name'])#test
print (s['type']['name'])#seq
print (s['type']['parameter'][1])#2

 

posted @ 2015-11-21 16:32  Xiao|Deng  阅读(170)  评论(0编辑  收藏  举报