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
原文地址:https://www.cnblogs.com/cfox/p/3411196.html