将Python列表值转换为Json

时间:2019-07-04 09:01:13

标签: python json

我想将Python列表转换为Json,我的python列表设置为key-> value。 但是,我所有的值有时必须是键,有时必须是json中的值。

例如:


import json


raw = [
    {
        "name": "User-Agent",
        "value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36"
    },
    {
        "name": "Accept",
        "value": "*/*"
    },
    {
        "name": "Accept-Language",
        "value": "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"
    },
    {
        "name": "Accept-Encoding",
        "value": "gzip, deflate, br"
    },
    {
        "name": "Content-Type",
        "value": "text/plain;charset=UTF-8"
    },
    {
        "name": "Content-Length",
        "value": "205"
    },
    {
        "name": "Connection",
        "value": "keep-alive"
    }
]


for i in raw:
    print (i['name'],i['value'])

我想要Json格式的东西

{
'User-Agent' :  'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36',
'Accept' :  '*/*',
'Accept-Language' : 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding'  : 'gzip, deflate, br',
'Content-Type' : 'text/plain;charset=UTF-8',
'Content-Length' : '205',
'Connection' : 'keep-alive'
}

谢谢

4 个答案:

答案 0 :(得分:3)

尝试一下

>>> {e['name']:e['value'] for e in raw}

输出:

{'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36', 
'Accept': '*/*', 
'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3', 
'Accept-Encoding': 'gzip, deflate, br',
 'Content-Type': 'text/plain;charset=UTF-8', 
'Content-Length': '205', 
'Connection': 'keep-alive'}

答案 1 :(得分:2)

尝试一下:

dict_ = {}
for i in raw:
    dict_[i['name']] = i['value']

或者,简单地:

dict_ = {i['name'] : i['value'] for i in raw}

输出

{
'User-Agent' :  'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36',
'Accept' :  '*/*',
'Accept-Language' : 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding'  : 'gzip, deflate, br',
'Content-Type' : 'text/plain;charset=UTF-8',
'Content-Length' : '205',
'Connection' : 'keep-alive'
}

答案 2 :(得分:0)

您可以尝试以下代码:

d1 = {item['name']:item['value'] for item in raw}
print(d1)

输出:

{
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36', 
'Accept': '*/*', 
'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'text/plain;charset=UTF-8',
'Content-Length': '205',
'Connection': 'keep-alive'
}

答案 3 :(得分:0)

import json
dict = {}
for item in raw:
    dict.update({item['name']:item['value']})
#dict = {item['name'] : item['value'] for item in raw}

print (dict)
print (json.dumps(dict)) # convert dictionary into json

输出:

{'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36', 'Accept': '*/*', 'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept-Encoding': 'gzip, deflate, br', 'Content-Type': 'text/plain;charset=UTF-8', 'Content-Length': '205', 'Connection': 'keep-alive'}
{"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36", "Accept": "*/*", "Accept-Language": "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3", "Accept-Encoding": "gzip, deflate, br", "Content-Type": "text/plain;charset=UTF-8", "Content-Length": "205", "Connection": "keep-alive"}