简体   繁体   English

如何将这个特定的json字符串转换为python字典?

[英]How to convert this particular json string into a python dictionary?

How do I convert this string -> 如何转换此字符串 - >

 string = [{"name":"sam"}]

into a python dictionary like so -> 进入像这样的python字典 - >

data = {
         "name" : "sam"
       }
In [1]: import json

In [2]: json.loads('[{"name":"sam"}]')
Out[2]: [{u'name': u'sam'}]

This returns a list, the first element of which is the desired dictionary. 这将返回一个列表,其中第一个元素是所需的字典。

string = [{ "name" : "sam" }]
data = string[0]

now the data is the dict 现在数据是dict

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM