2015-10-17 33 views
1

我刚刚制作了一个程序来解析api中的一些数据。 api以JSON格式返回数据。当我尝试分析它,它给了我一个关键的错误Python:解析JSON时得到Keyerror

Traceback (most recent call last): 
    File "test.py", line 20, in <module> 
    print(parsed_json['plain']) 
KeyError: 'plain' 

这是一个重要的部分代码(剩下的只是制作的URL,工作完全正常)

response = urllib.request.urlopen(url2).read() 
strr = str(response) 


if "plain" in strr: 
    parsed_json = json.loads(response.decode("UTF-8")) 
    print(parsed_json['plain']) 
elif "INVALID HASH" in strr: 
    print("You have entered an invalid hash.") 
elif "NOT FOUND" in strr: 
    print("The hash is not found") 
elif "LIMIT REACHED" in strr: 
    print("You have reached the max requests per minute, please try again in one minute.") 

我我试图在普通的领域获得数据。 下面是从API输出:

{ 
    "REQUEST": "FOUND", 
    "739c5b1cd5681e668f689aa66bcc254c": { 
    "plain": "test", 
    "hexplain": "74657374", 
    "algorithm": "MD5X5PLAIN" 
    } 
} 
+1

除非我失去了一些东西,它看起来像“plain”是“739c5b1cd5681e668f689aa66bcc254c”的子项。 –

+0

@MorganThrapp我想在平淡之后获得“测试”。不是本身。 – Uber

+0

'“test”'是“plain”键的值吗? –

回答

1

正是看到什么就当你可以看到你正在尝试的目标的内部数据的JSON对象的嵌套结构要容易得多:

工作实施例#1 - 与Python测试2.6.92.7.103.3.53.5.0

import json 

json_string = ''' 
{ 
    "REQUEST": "FOUND", 
    "739c5b1cd5681e668f689aa66bcc254c": { 
     "plain": "test", 
     "hexplain": "74657374", 
     "algorithm": "MD5X5PLAIN" 
    } 
} 
''' 

if 'plain' in json_string: 
    parsed_json = json.loads(json_string) 
    print(parsed_json['739c5b1cd5681e668f689aa66bcc254c']['plain']) 

“纯”是“739c5b1cd5681e668f689aa66bcc254c”


编辑的子

以下示例通过parsed_json并检查环路,用于32个字符的长度的每个键,并检查该键具有孩子的价值在'平原'里面。

工作实施例#2 - 与Python 2.6.92.7.103.3.53.5.0

import json 
import re 

def is_MD5(s): 
    return True if re.match(r"([a-f\d]{32})", key) else False 

strr = ''' 
{ 
    "REQUEST": "FOUND", 
    "739c5b1cd5681e668f689aa66bcc254c": { 
     "plain": "test", 
     "hexplain": "74657374", 
     "algorithm": "MD5X5PLAIN" 
    } 
} 
''' 

parsed_json = json.loads(strr) 

for key, value in parsed_json.items(): 
    if is_MD5(key) and 'plain' in parsed_json[key]: 
     xHash = key 
     xPlain = parsed_json[key]['plain'] 

     print('value in key "plain" in key "{0}" is "{1}"'.format(*[xHash, 
                    xPlain])) 

输出测试

the value of key "plain" in key "739c5b1cd5681e668f689aa66bcc254c" is "test" 
+0

这使我对它更加清楚!谢谢! – Uber

+0

一个问题,如果我可能会问,如果739c5b1cd5681e668f689aa66bcc254c是不同的?因为散列会一直有所不同,所以如何使它像动态一样,以便它抓住它呢? – Uber

+0

@ user3104326有关处理动态密钥的更优雅的解决方案,请参阅示例2。 – jesterjunk

1

在你的数据,'plain'不是parsed_json成员。它parsed_json['739c5b1cd5681e668f689aa66bcc254c']的成员。所以parsed_json['739c5b1cd5681e668f689aa66bcc254c']['plain']应该工作。

JSON是一种分层数据结构。顶级括号表示整个事物是将被分配到parsed_json的一个对象。每个成员都是名称 - 值对; 'REQUEST'的值是'FOUND'。然而,'739c5b1cd5681e668f689aa66bcc254c'的值是一个子对象,用左括号表示。其成员是'plain','hexplain''algorithm'。这应该是更清楚,如果我把它写这样的:

parsed_json: { 
    "REQUEST":"FOUND", 
    "739c5b1cd5681e668f689aa66bcc254c": { 
     "plain":"test", 
     "hexplain":"74657374", 
     "algorithm":"MD5X5PLAIN" 
    } 
} 
+0

谢谢,这工作,我现在得到测试。为什么它是一个成员,是否像一个标题或类似的东西(编程新手,从未使用json) – Uber