Coo*_*raw 6 python json python-3.x
我有以下json:
{
"slate" : {
"id" : {
"type" : "integer"
},
"name" : {
"type" : "string"
},
"code" : {
"type" : "integer",
"fk" : "banned.id"
}
},
"banned" : {
"id" : {
"type" : "integer"
},
"domain" : {
"type" : "string"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想找出最好的解码方式,让它具有易于浏览的python对象呈现方式.
我试过了:
import json
jstr = #### my json code above ####
obj = json.JSONDecoder().decode(jstr)
for o in obj:
for t in o:
print (o)
Run Code Online (Sandbox Code Playgroud)
但我得到:
f
s
l
a
t
e
b
a
n
n
e
d
Run Code Online (Sandbox Code Playgroud)
我不明白这是什么交易.理想的是一棵树(甚至以树的方式组织的列表),我可以以某种方式浏览:
for table in myList:
for field in table:
print (field("type"))
print (field("fk"))
Run Code Online (Sandbox Code Playgroud)
Python的内置JSON API范围是否足以达到此预期?
Tha*_*tos 11
您似乎需要帮助迭代返回的对象,以及解码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))
Run Code Online (Sandbox Code Playgroud)
尝试
obj = json.loads(jstr)
Run Code Online (Sandbox Code Playgroud)
代替
obj = json.JSONDecoder(jstr)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24686 次 |
| 最近记录: |