【问题标题】:I can get specific object with json dict but not json array [duplicate]我可以使用json dict而不是json数组获取特定对象[重复]
【发布时间】:2017-07-23 00:29:16
【问题描述】:

这个json文件:

[ {
  "irc_server" : null,
  "ssh_keys" : [ ],
  "branches" : {
    "master" : {
      "running_builds" : [ ],
      "recent_builds" : [ {
        "outcome" : "no_tests",
        "status" : "no_tests",
        "build_num" : 1,
        "vcs_revision" : "748f8679cfc850582e3ada08210d8566a291328c",
        "pushed_at" : "2017-07-21T03:39:46.230Z",
        "added_at" : "2017-07-21T03:36:24.657Z"
      } ],
      "last_non_success" : {
        "outcome" : "no_tests",
        "status" : "no_tests",
        "build_num" : 1,
        "vcs_revision" : "748f8679cfc850582e3ada08210d8566a291328c",
        "pushed_at" : "2017-07-21T03:39:46.230Z",
        "added_at" : "2017-07-21T03:36:24.657Z"
      }
    }
  }]

我如何解析它:

        projectsurl = get(API_PATH['PROJECTS'].format(self._token))  #get json data from url

        decodedjson = projectsurl.content.decode('utf-8') #decode to string

        jsondata = json.loads(decodedjson) #json obj

        return jsondata['vcs_revision']

如何从该数组中获取特定对象?每当我打电话给return jsondata['vcs_revision'] 它说我需要一个 int 而不是字符串

【问题讨论】:

    标签: python arrays json dictionary


    【解决方案1】:

    你需要做jsondata[0]['vcs_revision'] 因为你的jsondata 是一个字典列表。

    【讨论】:

    • 解决了这个问题,我应该删除这个问题还是保留它?
    • 太棒了。如何处理这个问题取决于你。如果你离开它,没人会在意。
    猜你喜欢
    • 2020-03-01
    • 2017-12-16
    • 1970-01-01
    • 2021-05-17
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 2020-08-03
    相关资源
    最近更新 更多