0

我有一个 Python 脚本,可以调用 API 从 Zendesk 检索数据。(使用 Python 3.x)JSON 对象的结构如下:

{
  "id":               35436,
  "url":              "https://company.zendesk.com/api/v2/tickets/35436.json",
  "external_id":      "ahg35h3jh",
  "created_at":       "2009-07-20T22:55:29Z",
  "updated_at":       "2011-05-05T10:38:52Z",
  "type":             "incident",
  "subject":          "Help, my printer is on fire!",
  "raw_subject":      "{{dc.printer_on_fire}}",
  "description":      "The fire is very colorful.",
  "priority":         "high",
  "status":           "open",
  "recipient":        "support@company.com",
  "requester_id":     20978392,
  "submitter_id":     76872,
  "assignee_id":      235323,
  "organization_id":  509974,
  "group_id":         98738,
  "collaborator_ids": [35334, 234],
  "forum_topic_id":   72648221,
  "problem_id":       9873764,
  "has_incidents":    false,
  "due_at":           null,
  "tags":             ["enterprise", "other_tag"],
  "via": {
    "channel": "web"
  },
  "custom_fields": [
    {
      "id":    27642,
      "value": "745"
    },
    {
      "id":    27648,
      "value": "yes"
    }
  ],
  "satisfaction_rating": {
    "id": 1234,
    "score": "good",
    "comment": "Great support!"
  },
  "sharing_agreement_ids": [84432]
}

我遇到问题的地方是"custom_fields"具体的部分。我在每张票中都有一个特定的自定义字段,我需要它的值,我只想要那个特定的值。

为了省去 Python 代码的太多细节,我正在阅读下面每张票的每个值,并将其添加到输出变量中,然后再将该输出变量写入 .csv。这是发生破损的特定位置:

output += str(ticket['custom_fields'][id:23825198]).replace(',', '')+',' 

所有的替换废话都是为了确保因为它进入一个逗号分隔的文件,所以值中的任何逗号都被删除了。无论如何,这是我得到的错误:

    output += str(ticket['custom_fields'][id:int(23825198)]).replace(',', '')+','
TypeError: slice indices must be integers or None or have an __index__ method

如您所见,我已经尝试了几种不同的变体来尝试解决问题,但尚未找到解决方法。我可以使用一些帮助!

谢谢...

4

1 回答 1

0

你在使用 json.loads() 吗?如果是这样,您可以获得密钥,并对密钥执行 if 语句。下面显示了如何获取密钥及其各自值的示例。

import json

some_json = """{
"id":               35436,
"url":              "https://company.zendesk.com/api/v2/tickets/35436.json",
"external_id":      "ahg35h3jh",
"created_at":       "2009-07-20T22:55:29Z",
"updated_at":       "2011-05-05T10:38:52Z",
"type":             "incident",
"subject":          "Help, my printer is on fire!",
"raw_subject":      "{{dc.printer_on_fire}}",
"description":      "The fire is very colorful.",
"priority":         "high",
"status":           "open",
"recipient":        "support@company.com",
"requester_id":     20978392,
"submitter_id":     76872,
"assignee_id":      235323,
"organization_id":  509974,
"group_id":         98738,
"collaborator_ids": [35334, 234],
"forum_topic_id":   72648221,
"problem_id":       9873764,
"has_incidents":    false,
"due_at":           null,
"tags":             ["enterprise", "other_tag"],
"via": {
    "channel": "web"
},
"custom_fields": [
    {
    "sid":    27642,
    "value": "745"
    },
    {
    "id":    27648,
    "value": "yes"
    }
],
"satisfaction_rating": {
    "id": 1234,
    "score": "good",
    "comment": "Great support!"
},
"sharing_agreement_ids": [84432]
}"""

# load the json object
zenJSONObj = json.loads(some_json)

# Shows a list of all custom fields
print("All the custom field data")
print(zenJSONObj['custom_fields'])
print("----")
# Tells you all the keys in the custom_fields
print("How keys and the values")
for custom_field in zenJSONObj['custom_fields']:
    print("----")
    for key in custom_field.keys():
        print("key:",key," value: ",custom_field[key])

然后,您可以通过执行类似的操作来修改 JSON 对象

print(zenJSONObj['custom_fields'][0])
zenJSONObj['custom_fields'][0]['value'] = 'something new'
print(zenJSONObj['custom_fields'][0])

然后使用以下代码重新编码:

newJSONObject = json.dumps(zenJSONObj, sort_keys=True, indent=4)

我希望这会有所帮助。

于 2014-11-25T22:14:38.920 回答