使用Python将字符串转换为JSON。

时间:2022-12-20 20:13:23

I'm a little bit confused with JSON in Python. To me, it seems like a dictionary, and for that reason I'm trying to do that:

在Python中,我有点困惑于JSON。对我来说,它就像一本字典,因此我正试图这样做:

{
    "glossary":
    {
        "title": "example glossary",
        "GlossDiv":
        {
            "title": "S",
            "GlossList":
            {
                "GlossEntry":
                {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef":
                    {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

But when I do print dict(json), it gives an error.

但是当我执行print dict(json)时,它会出错。

How can I transform this string into a structure and then call json["title"] to obtain "example glossary"?

如何将该字符串转换为结构,然后调用json["title"]来获得“示例词汇表”?

5 个解决方案

#1


419  

json.loads()

json.loads()

d = json.loads(j)
print d['glossary']['title']

#2


68  

When I started using json, I was confused and unable to figure it out for some time, but finally I got what I wanted
Here is the simple solution

当我开始使用json时,我感到很困惑,并不能解决这个问题,但最终我得到了我想要的这个简单的解决方案。

import json
m = {'id': 2, 'name': 'hussain'}
n = json.dumps(m)
o = json.loads(n)
print o['id'], o['name']    

#3


15  

use simplejson or cjson for speedups

使用simplejson或cjson来实现速度。

import simplejson as json

json.loads(obj)

or 

cjson.decode(obj)

#4


0  

If you trust the data source, you can use eval to convert your string into a dictionary:

如果您信任数据源,则可以使用eval将字符串转换为字典:

eval(your_json_format_string)

eval(your_json_format_string)

Example:

例子:

>>> x = "{'a' : 1, 'b' : True, 'c' : 'C'}"
>>> y = eval(x)

>>> print x
{'a' : 1, 'b' : True, 'c' : 'C'}
>>> print y
{'a': 1, 'c': 'C', 'b': True}

>>> print type(x), type(y)
<type 'str'> <type 'dict'>

>>> print y['a'], type(y['a'])
1 <type 'int'>

>>> print y['a'], type(y['b'])
1 <type 'bool'>

>>> print y['a'], type(y['c'])
1 <type 'str'>

#5


-1  

There is a way without JSON in this answer.

在这个答案中有一种没有JSON的方法。

However, this way requires a fine tuning on data structure

但是,这种方式需要对数据结构进行微调。

#1


419  

json.loads()

json.loads()

d = json.loads(j)
print d['glossary']['title']

#2


68  

When I started using json, I was confused and unable to figure it out for some time, but finally I got what I wanted
Here is the simple solution

当我开始使用json时,我感到很困惑,并不能解决这个问题,但最终我得到了我想要的这个简单的解决方案。

import json
m = {'id': 2, 'name': 'hussain'}
n = json.dumps(m)
o = json.loads(n)
print o['id'], o['name']    

#3


15  

use simplejson or cjson for speedups

使用simplejson或cjson来实现速度。

import simplejson as json

json.loads(obj)

or 

cjson.decode(obj)

#4


0  

If you trust the data source, you can use eval to convert your string into a dictionary:

如果您信任数据源,则可以使用eval将字符串转换为字典:

eval(your_json_format_string)

eval(your_json_format_string)

Example:

例子:

>>> x = "{'a' : 1, 'b' : True, 'c' : 'C'}"
>>> y = eval(x)

>>> print x
{'a' : 1, 'b' : True, 'c' : 'C'}
>>> print y
{'a': 1, 'c': 'C', 'b': True}

>>> print type(x), type(y)
<type 'str'> <type 'dict'>

>>> print y['a'], type(y['a'])
1 <type 'int'>

>>> print y['a'], type(y['b'])
1 <type 'bool'>

>>> print y['a'], type(y['c'])
1 <type 'str'>

#5


-1  

There is a way without JSON in this answer.

在这个答案中有一种没有JSON的方法。

However, this way requires a fine tuning on data structure

但是,这种方式需要对数据结构进行微调。