Python 解码 JSON

发布于 2024-10-25 22:28:41 字数 1008 浏览 2 评论 0原文

我有以下 json:

{
    "slate" : {
        "id" : {
            "type" : "integer"
        },
        "name" : {
            "type" : "string"
        },
        "code" : {
            "type" : "integer",
            "fk" : "banned.id"
        }
    },
    "banned" : {
        "id" : {
            "type" : "integer"
        },
        "domain" : {
            "type" : "string"
        }
    }
}

我想找出最佳解码方式来轻松浏览它的 python 对象表示。

我尝试过:

import json

jstr = #### my json code above #### 
obj = json.JSONDecoder().decode(jstr)

for o in obj:
  for t in o: 
    print (o)

但我得到:

    f       
    s
    l
    a
    t
    e
    b
    a
    n
    n
    e
    d

我不明白这是怎么回事。理想的情况是一棵树(甚至是以树的方式组织的列表),我可以以某种方式浏览它,例如:

for table in myList:
    for field in table:
         print (field("type"))
         print (field("fk"))  

Python 的内置 JSON API 范围是否足够宽以达到此期望?

I've the following json:

{
    "slate" : {
        "id" : {
            "type" : "integer"
        },
        "name" : {
            "type" : "string"
        },
        "code" : {
            "type" : "integer",
            "fk" : "banned.id"
        }
    },
    "banned" : {
        "id" : {
            "type" : "integer"
        },
        "domain" : {
            "type" : "string"
        }
    }
}

I'd like to figure out the best decoding way to have an easily browsable python object presentation of it.

I tried:

import json

jstr = #### my json code above #### 
obj = json.JSONDecoder().decode(jstr)

for o in obj:
  for t in o: 
    print (o)

But I get:

    f       
    s
    l
    a
    t
    e
    b
    a
    n
    n
    e
    d

And I don't understand what's the deal. The ideal would be a tree (even a list organized in a tree way) that I could browse somehow like:

for table in myList:
    for field in table:
         print (field("type"))
         print (field("fk"))  

Is the Python's built-in JSON API extent wide enough to reach this expectation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

牵你手 2024-11-01 22:28:41

您似乎需要帮助迭代返回的对象以及解码 JSON。

import json

#jstr = "... that thing above ..."
# This line only decodes the JSON into a structure in memory:
obj = json.loads(jstr)
# obj, in this case, is a dictionary, a built-in Python type.

# These lines just iterate over that structure.
for ka, va in obj.iteritems():
    print ka
    for kb, vb in va.iteritems():
        print '  ' + kb
        for key, string in vb.iteritems():
            print '    ' + repr((key, string))

You seem to need help iterating over the returned object, as well as decoding the JSON.

import json

#jstr = "... that thing above ..."
# This line only decodes the JSON into a structure in memory:
obj = json.loads(jstr)
# obj, in this case, is a dictionary, a built-in Python type.

# These lines just iterate over that structure.
for ka, va in obj.iteritems():
    print ka
    for kb, vb in va.iteritems():
        print '  ' + kb
        for key, string in vb.iteritems():
            print '    ' + repr((key, string))
哎呦我呸! 2024-11-01 22:28:41

尝试

obj = json.loads(jstr)

代替

obj = json.JSONDecoder(jstr)

Try

obj = json.loads(jstr)

instead of

obj = json.JSONDecoder(jstr)
梦醒时光 2024-11-01 22:28:41

我猜想的交易是您创建一个解码器,但永远不要告诉它 <代码>解码()

使用:

o = json.JSONDecoder().decode(jstr)

The deal I guess is that you create a decoder, but never tell it to decode().

Use:

o = json.JSONDecoder().decode(jstr)
只是在用心讲痛 2024-11-01 22:28:41

JSONDecoder 的签名是

class json.JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, 
    parse_constant[, strict[, object_pairs_hook]]]]]]])

且不接受构造函数中的 JSON 字符串。看看它的decode()方法。

http://docs.python.org/library/json.html#json.JSONDecoder

The signature of JSONDecoder is

class json.JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, 
    parse_constant[, strict[, object_pairs_hook]]]]]]])

and does not accept the JSON string in the constructur. Look at its decode() method.

http://docs.python.org/library/json.html#json.JSONDecoder

堇色安年 2024-11-01 22:28:41

这对我来说效果很好,而且打印比显式循环对象更简单,如 Thanatos 的答案

import json
from pprint import pprint

jstr = #### my json code above #### 
obj = json.loads(jstr)

pprint(obj)

这使用“Data Pretty Printer”(pprint) 模块,其文档可以在 此处

This worked well for me, and the printing is simpler than explicitly looping through the object like in Thanatos' answer:

import json
from pprint import pprint

jstr = #### my json code above #### 
obj = json.loads(jstr)

pprint(obj)

This uses the "Data Pretty Printer" (pprint) module, the documentation for which can be found here.

孤檠 2024-11-01 22:28:41

您在示例中提供的字符串不是有效的 JSON。

两个右大括号之间的最后一个逗号是非法的。

无论如何,你应该遵循 Sven 的建议并使用负载。

The String you provide in the example is not valid JSON.

The last comma between two closing curly braces is illegal.

Anyway you should follow Sven's suggestion and use loads instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文