使用Python将字符串转换为JSON [英] Convert string to JSON using Python

查看:882
本文介绍了使用Python将字符串转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有点混淆了JSON。
对我来说,它似乎是一个字典,为此原因
我试图这样做:

  {
glossary:
{
title:example glossary,
GlossDiv:
{
title :S,
GlossList:
{
GlossEntry:
{
ID:SGML,
SortAs :SGML,
GlossTerm:标准通用标记语言,
首字母缩略词:SGML,
Abbrev:ISO 8879:1986,
GlossDef:
{
para:用于创建标记语言(如DocBook)的元标记语言,
GlossSeeAlso:[GML, XML]
},
GlossSee:标记
}
}
}
}
}

但是当我打印dict(json)时,会给出错误。



如何将此字符串转换成一个结构,然后调用json [title]获取示例词汇表?

解决方案

json.loads()

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


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:

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

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

解决方案

json.loads()

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

这篇关于使用Python将字符串转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆