Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python解码JSON_Python_Json_Python 3.x - Fatal编程技术网

Python解码JSON

Python解码JSON,python,json,python-3.x,Python,Json,Python 3.x,我有以下json: { "slate" : { "id" : { "type" : "integer" }, "name" : { "type" : "string" }, "code" : { "type" : "integer", "fk" : "banned.id" } },

我有以下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范围是否足够宽以达到这一预期

试试看

obj = json.loads(jstr)
而不是

obj = json.JSONDecoder(jstr)

我想你应该创建一个解码器,但决不告诉它

使用:


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

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


无论如何,您应该遵循Sven的建议,使用loads来代替

class json.JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, 
    parse_constant[, strict[, object_pairs_hook]]]]]]])
并且不接受构造函数中的JSON字符串。看看它的decode()方法


您似乎需要帮助迭代返回的对象,以及解码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))

这对我来说效果很好,打印比在对象中显式循环更简单,如:


这使用了“数据漂亮打印机”(
pprint
)模块,可以找到该模块的文档。

很好。但是:对于对象中的o:for t in o:print(t)sla t e b a n e d对于JSON,Python将返回一个
dict
(dictionary)对象。默认情况下,对其进行迭代将获得键,这些键是字符串。对字符串进行迭代可以获得个字符。您可以使用
.iteritems()
获取
(键、值)
的元组,或者使用
.itervalues()
仅获取字典上for循环中的值。@coolstrow,decode()的结果是一个python字典。您可能需要遍历o.items(),因为您正在遍历键,然后遍历键中的字母,我修复了它。但我还是不能像预期的那样浏览。我要更新我的问题这是正确的方法
json.loads()
从json对象返回一个Python字典。@Blairg23如何处理已知格式的json?@Jeremy如果知道数据结构,只需引用键即可获得值。在这个例子中,他使用了一个三重嵌套的for循环,他可以这样说:
slate\u id\u type=obj['slate']['id']['type']
。既然我们假设他在用这些值做某些事情,那么最好在这里声明它们。“无论如何,直截了当要好得多。”杰里米绝对是!顺便说一句,不要忘记放置
try
,除了
块以捕获任何可能不存在的键值。查看(如果您正在使用2.7,请更改为2.7)了解更多异常选项。
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))
import json
from pprint import pprint

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

pprint(obj)