zoukankan      html  css  js  c++  java
  • Python学习Json

    Json格式

    {
        "all": [
            {
                "sum": 36,
                "score0": 4,
                "score1": 32,
                "score2": 0
            }
        ],
        "strategy_0": [
            {
                "missing": 3,
                "dead": 0,
                "error": 0,
                "submit": 1,
                "page": 0
            }
        ],
        "strategy_1": [
            {
                "img": 0,
                "flash": 0,
                "blank": 0,
                "nav": 33,
                "login": 6
            }
        ],
        "strategy_2": [
            {
                "nave": 0,
                "textpaget": 0,
                "register": 30,
                "content": 0,
                "form": 35
            }
        ]
    }

    把json串联成字符串,在python中解析

    #!/usr/bin/env python
    import json
    
    json_str = '{ sum:[{"value1":3, "value2":4}], strategy:[{"stgy1":2, "stgy2":3}] }'
    
    sum = []
    list_sum = [] decode
    = json.loads()
    for val in decode:
    sum = decode.get(val, list_sum)

    dic1 = sum[0]
    for key in dic1:
    print dic1.get(key, 0) #这里就可以打印出sum下的value1和value2的值。 3,4

    下面是几个列表转换成json格式

    #!/usr/bin/env python
    import json
    
    sumlist = [{'value1':1, 'value2':2}]
    strategy = [{'stgy1'}:1, 'stgy':2]
    
    def convert_to_json():
        json_str = '{ "sum" :'
        json_str += json.dumps(sumlist)
        json_str += ', "strategy" : '
        json_str += json.dumps(strategy)
        json_str += '}'
        return json_str
  • 相关阅读:
    C语言寒假大作战02
    C语言寒假大作战01
    C语言I作业12
    C语言I博客作业11
    C语言I博客作业10
    C语言I博客作业09
    C语言I作业08
    C语言ㅍ作业01 结构:通讯录
    C语言寒假大作战04
    C语言寒假大作战03
  • 原文地址:https://www.cnblogs.com/cfox/p/3411196.html
Copyright © 2011-2022 走看看