需要帮助使用python从JSON中提取值,获取TypeError

时间:2015-06-29 17:58:40

标签: python json keyerror

以下是我从网址中提取的JSON示例:

[
{
    "externalModelId": "500A000000RQOwnIAH", 
    "resource": {
        "account": {
            "externalModelId": "001A000001EucpoIAB", 
            "resource": {
                "accountName": "Foobar", 
                "accountNumber": 1234567, 
            }, 
            "resourceReliability": "Fresh"
        }, 
        "caseNumber": 1234567, 
        "created": "2015-06-12T19:06:22.000Z", 
        "createdBy": {
            "externalModelId": "005A0000005mhdXIAQ", 
            "resourceReliability": "Fresh"
        }, 
        "description": "Example description", 
        "hoursInCurrentStatus": 406, 
        "internalPriority": "3 (Normal)", 
        "lastModified": "2015-06-22T14:08:18.000Z", 
        "owner": {
            "externalModelId": "005A0000001sKDzIAM", 
            "resourceReliability": "Fresh"
        }, 
        "product": {
            "resource": {
                "line": {
                    "externalModelId": 21118, 
                    "resource": {
                        "name": null
                    }, 
                    "resourceReliability": "Fresh"
                }, 
                "version": {
                    "externalModelId": 21988, 
                    "resource": {
                        "name": "1.2"
                    }, 
                    "resourceReliability": "Fresh"
                }
            }, 
            "resourceReliability": "Fresh"
        }, 
        "resourceCount": 0, 
        "sbrs": [
            "Value"
        ], 
        "sbt": 139, 
        "severity": "4 (Low)", 
        "status": "Status Example", 
        "subject": "Subject Example", 
        "tags": [
            "br", 
            "fs"
        ], 
        "targetDate": "2015-07-15T17:46:48.000Z", 
        "type": "Feature"
    }, 
    "resourceReliability": "Fresh"
}, 

我有兴趣从中提取以下数据:

  • caseNumber
  • subject
  • 严重性
  • sbt
  • sbrs
  • 状态

我目前的代码是:

#!/usr/bin/env python  
import sys
import requests
import json
import os

# Setup 
username = "XXX"
password = "XXX"
accountid = "12345"

# Formulate the string and then capture the output
url = "http://XXX{0}XXX{1}XXXXX".format(accountid, filedtime)
r = requests.get(url, auth=(username, password))
parsed = json.loads(r.text)
parent = parsed['resource']

# Using json_string for testing
#json_string = json.dumps(parsed, indent=4, sort_keys=True)
#print json_string

for item in parent:
    print item['caseNumber']
    print item['subject']
    print item['severity']
    print item['sbt']
    print item['sbrs']
    print item['status']

代码输出TypeError:

Traceback (most recent call last):
  File "./newcase-notify.py", line 31, in <module>
    parent = parsed['resource']
TypeError: list indices must be integers, not str

我尝试过指定类似的内容:

parent = parsed['resource'][0]['type']

但这不起作用。我想我现在很困惑。如果我没有指定父级,只需遍历'解析',如:

for item in parsed:
    print item['caseNumber']
    print item['subject']
    print item['severity']
    print item['sbt']
    print item['sbrs']
    print item['status']

我再次得到了KeyError。

我的问题:

根据提供的信息,我如何从我的JSON对象中提取上述值?

2 个答案:

答案 0 :(得分:1)

我通过删除:

解决了这个问题
WebApplication

并使用:

parent = parsed['resource']

答案 1 :(得分:0)

如果您查看JSON的顶部,您会注意到这一点:

[
{

这意味着一个数组,里面有一个对象。您需要首先从阵列中取消引用该对象。因此,为什么你得到关于列表索引的爵士乐必须是整数而不是字符串。一旦你这样做它应该工作。

parent = parsed[0]['resource']应该会解决你的问题。

只是为了帮助指导您在命名法之间进行翻译: 数组:JS作为List:Python和Object:JS作为Dict:Python。