• 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
  • 相关阅读:
    socket.io建立长连接
    编译性语言、解释性语言和脚本语言的区别
    回调函数
    jquery文档处理
    css--Bootstrap框架
    css--960框架
    html基础
    真的理解闭包了吗,用闭包的时候应该注意什么?
    初识reactJs 相关
    简单方便的div垂直居中。
  • 原文地址:https://www.cnblogs.com/cfox/p/3411196.html
Copyright © 2020-2023  润新知