2012-05-10 79 views
1

我试图在不知道键名称的情况下从json响应中打印所有“键值”(例如,不使用语法json ['example'])。我与使用iteritems()递归函数这样做,但我有一些问题:用python读取Json响应

这是JSON响应,我试图阅读:

{"servers": [{"id": "a059eccb-d929-43b2-8db3-b32b6201d60f", "links": [{"href": "http://192.168.100.142:8774/v2/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "self"}, {"href": "http://192.168.100.142:8774/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "bookmark"}], "name": "birk"}]} 

这是funcion我使用:

def format_main_response(self, json_string): 
    print "json:  " + json_string 
    content = json.loads(str(json_string)) 
    for key, value in content.iteritems(): 
     print key 
     if type(value) == type(['']): 
      strg = str(json.dumps(value)) 
      strg = strg.strip('[]') 
      self.format_main_response(strg) 
     else: 
      print value 

我使用的是带功能,采取了所有的“[]”从我的JSON字符串。如果我没有这样做,我尝试使用'json.loads()'函数加载它时出现错误。

Traceback (most recent call last): 
     File "main.py", line 135, in <module> 
     formatter.format_main_response(nova_API.list_servers()) 
     File "/home/python/jsonformatter.py", line 51, in format_main_response 
     self.format_main_response(strg, mod) 
     File "/home/python/jsonformatter.py", line 51, in format_main_response 
     self.format_main_response(strg, mod) 
     File "/home/python/jsonformatter.py", line 31, in format_main_response 
     for key, value in content.iteritems(): 
    AttributeError: 'list' object has no attribute 'iteritems' 

我的问题是,在某些时候应打印看起来像这样的JSON,没有“[]”:

{"href": "http://192.168.100.142:8774/v2/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "self"}, {"href": "http://192.168.100.142:8774/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "bookmark"} 

当函数试图找到“键,值”从这个JSON,我得到这个错误:

Traceback (most recent call last): File "main.py", line 135, in <module> 
    formatter.format_main_response(nova_API.list_servers()) 
    File "/home/python/jsonformatter.py", line 34, in format_main_response 
    self.format_main_response(strg) 
    File "/home/python/jsonformatter.py", line 34, in format_main_response 
    self.format_main_response(strg) 
    File "/home/python/jsonformatter.py", line 28, in format_main_response 
    content = json.loads(str(json_string)) 
    File "/usr/lib/python2.7/json/__init__.py", line 326, in loads 
    return _default_decoder.decode(s) 
    File "/usr/lib/python2.7/json/decoder.py", line 369, in decode 
    raise ValueError(errmsg("Extra data", s, end, len(s))) 
ValueError: Extra data: line 1 column 135 - line 1 column 273 (char 135 - 273) 

任何人都知道我应该在这种情况下做什么?或以其他方式获得相同的结果?

回答

2

使用:

def format_main_response(json_string): 
    print "json:  " + json_string 
    content = json.loads(str(json_string)) 
    for key, value in content.iteritems(): 
     print key 
     if type(value) == type(['']): 
      for sub_value in value: 
       strg = str(json.dumps(sub_value)) 
       format_main_response(strg) 
     else: 
      print value 

这就是结果:

~$ python test_pdb.py 
json:  {"servers": [{"id": "a059eccb-d929-43b2-8db3-b32b6201d60f", "links": [{"href": "http://192.168.100.142:8774/v2/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "self"}, {"href": "http://192.168.100.142:8774/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "bookmark"}], "name": "birk"}]} 
servers 
json:  {"id": "a059eccb-d929-43b2-8db3-b32b6201d60f", "links": [{"href": "http://192.168.100.142:8774/v2/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "self"}, {"href": "http://192.168.100.142:8774/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "bookmark"}], "name": "birk"} 
id 
a059eccb-d929-43b2-8db3-b32b6201d60f 
links 
json:  {"href": "http://192.168.100.142:8774/v2/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "self"} 
href 
http://192.168.100.142:8774/v2/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f 
rel 
self 
json:  {"href": "http://192.168.100.142:8774/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "bookmark"} 
href 
http://192.168.100.142:8774/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f 
rel 
bookmark 
name 
birk 
+0

非常感谢!这解决了我的问题 – Birk

0

如何:

jsonStr = {"href": "http://192.168.100.142:8774/v2/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "self"}, {"href": "http://192.168.100.142:8774/2ad1fc162c254e59bea043560b7f73cb/servers/a059eccb-d929-43b2-8db3-b32b6201d60f", "rel": "bookmark"} 
print json.dumps(jsonStr, sort_keys=True, indent=2, separators=(',', ': ')) 

这应该给你想要的下面

0

代码递归格式遍历json响应并打印键,值对: 诀窍是在主只有一次加载JSON响应,然后递归遍历所述响应:

def parse_json_response(content): 

    if len (content.keys()) > 1 : 
     for key, value in content.iteritems(): 
      print "key : ", key 
      print "Value", value 

      if type(value) is dict: 
       parse_json_response(value) 
    else: 
     print value 

if __name__ == '__main__': 

    content = json.loads(str(response)) 
    parse_json_response(content) 

希望它能帮助。