Convertir cadena a JSON usando Python


Estoy un poco confundido con JSON en Python. Para mí, parece un diccionario, y por esa razón Estoy tratando de hacer eso:

{
    "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"
                }
            }
        }
    }
}

Pero cuando imprimo dict(json), da un error.

¿Cómo puedo transformar esta cadena en una estructura y luego llamar a json["title"] para obtener "example glossary"?

Author: altocumulus, 2010-12-24

4 answers

json.loads()

import json

d = json.loads(j)
print d['glossary']['title']
 490
Author: Ignacio Vazquez-Abrams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-24 08:28:23

Cuando comencé a usar json, estaba confundido e incapaz de averiguarlo durante algún tiempo, pero finalmente obtuve lo que quería
Aquí está la solución simple

import json
m = {'id': 2, 'name': 'hussain'}
n = json.dumps(m)
o = json.loads(n)
print o['id'], o['name']    
 74
Author: Hussain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-23 09:14:52

Utilice simplejson o cjson para acelerar

import simplejson as json

json.loads(obj)

or 

cjson.decode(obj)
 15
Author: locojay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-26 20:33:36

Si confías en la fuente de datos, puedes usar eval para convertir tu cadena en un diccionario:

eval(your_json_format_string)

Ejemplo:

>>> 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'>
 4
Author: kakhkAtion,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-19 22:49:03