zoukankan      html  css  js  c++  java
  • python json模块

    json 模块提供了一种很简单的方式来编码和解码JSON数据。其中两个主要函数是
    
    json.dumps()和json.loads(),
    
    
    字典转json:
    
    import json
    data ={
        'name':'111',
        'shares':'222',
        'price':'3333'
    }
    print data
    print type(data)
    json_str=json.dumps(data)
    print json_str
    print type(json_str)
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a40.py
    {'price': '3333', 'name': '111', 'shares': '222'}
    <type 'dict'>
    {"price": "3333", "name": "111", "shares": "222"}
    <type 'str'>
    
    JSON 编码支持的基本数据类型为None,bool,int,float和str,以及包含这些类型数据的lists,tuples和字典
    
    
    为了遵循JSON规范,你应该只
    编码Python的lists和dictionaries。 而且,在web应用程序中,顶层对象被编码为一个字典
    是一个标准做法。
    
    列表转换为Json
    
    import json
    data=['a','b','c']
    print data
    print type(data)
    json_str=json.dumps(data)
    print json_str
    print type(json_str)
    
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a40.py
    ['a', 'b', 'c']
    <type 'list'>
    ["a", "b", "c"]
    <type 'str'>
    
    JSON 编码的格式对于Python语法几乎是完全一样的,除了一些小的差异之外。
    
    比
    如,True会被映射为true,False被映射为false,而None会被映射为null。 下面是一个例
    子,演示了编码后的字符串效果:
    
    import json
    d={'a':True,
       'b':'Hello',
        'c':None,
         'd':False}
    print json.dumps(d)
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a40.py
    {"a": true, "c": null, "b": "Hello", "d": false}
    
    
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import json
    import urllib
    import urllib2
    url = "https://www.zjcap.cn/web/noauth?productSn=1079&method=%2Fproduct%2Fdetail&platform=web&_=1509514340879"
    req = urllib2.Request(url)
    print req
    print type(req)
    
    res_data = urllib2.urlopen(req)
    res = res_data.read()
    print res
    print '-------type------'
    #类型为json
    print type(res)
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a40.py
    <urllib2.Request instance at 0x025B2620>
    <type 'instance'>
    {"data":{"bonus":0.0100000000,"buyToTime":"2017-10-31 16:00:00","precision":0,"buyToTimeMillis":1509436800000,"containBonus":"2","wzeCardYield":0E-10,"realValueStartDate":"2017-10-31","naturalSeasonRepaymentDay":0,"createdTime":"2017-10-31 11:07:45","showType":"2","supportCouponType":"","bonusType":"1","realValueEndDate":"2017-12-27","allowInvest":1,"unitValue":1.0000000000,"quotaForFirst":0,"assignTypeCode":1,"displayText":"1.00%","buyFromTime":"2017-10-31 11:06:00","yieldForFirst":0E-10,"delegator":"浙江省对外经济贸易投资有限公司","repaymentInterval":0,"custody":"中均资产管理有限公司","settlementDate":"2017-12-28","financeType":"1","name":"中均-尊享534号","totalQuota":4510000,"status":4,"realSettlementDate":"2017-12-28","buyAcctTypes":"1","isOnlyForFirst":"1","wzeCardYieldOnOff":"2","remark":"“中均-尊享534号”预期借款利率达7%/年(外加红包),期限预计为58个自然日,借款用途为补充该企业生产经营所需流动资金。","addUnit":10000,"supportStock":2,"accessType":1,"duration":58,"yield":0.0800000000,"maxUnit":5000000,"sn":1079,"productType":"3","daysOfYear":360,"riskInfo":"","buyFromTimeMillis":1509419160000,"assignType":"一次性还本付息","valueStartDate":"2017-10-31","stageNum":1,"progress":1.0000,"remainQuota":0,"entryUnit":1000000,"serverTimeMillis":1509520371647,"activityType":0,"valueEndDate":"2017-12-27"},"retCode":"0000","retMsg":"查询成功"}
    -------type------
    <type 'str'>
    
    
    
    
    
    python 解json:
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    from pprint import pprint
    import json
    import urllib
    import urllib2
    url = "https://www.zjcap.cn/web/noauth?productSn=1079&method=%2Fproduct%2Fdetail&platform=web&_=1509514340879"
    req = urllib2.Request(url)
    print req
    print type(req)
    
    res_data = urllib2.urlopen(req)
    res = res_data.read()
    print res
    print '-------type------'
    #类型为json
    print type(res)
    print '-------json--------'
    r=json.loads(res)
    print r
    print type(r)
    print r['data']['remark']
    
    
    下面如何将一个JSON字典转换为一个Python对象例子:
    

  • 相关阅读:
    getline在windows系统下的怪现象
    android camera根据屏幕图像大小设置显示
    android camera以时间来命名照片文件
    android 基本的画图方法
    android 开发使用剪切板传递数据
    用keytool生成一个自签名证书,并导出到文件
    VBA,两个sheet根据第一列id相同比较其余列,不同的值放到sheet3中
    摄像头视频采集压缩及传输
    Extjs 3根据数据库动态生成checkboxgroup(extjs系列1)
    Extjs 3,jstl根据数据库数据自动构建FormPanel(Extjs系列2)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349463.html
Copyright © 2011-2022 走看看