0

我有以下 json 对象数据,如何使用 python 来获取"BuildId"第二个"ftp" (注意:它在"ArchiveLocation")值?

     {"BuildId":"4c53575f-36f48a7f1f37",
      "EventId":1,
      "ArchiveLocations":
      [
         {
            "archive_server":"CA",
            "ftp":"ftp://ABC1.com/2011_V2/2011.4.103.002",
            "dfs":"\\\\ABD\\dfs\\Department\\2011.4.103.002"
         },
         {
            "archive_server":"CT",
            "ftp":"ftp://ABC2.com/2011_V2/2011.4.103.002",
            "dfs":"\\\\ABD.com\\dfs\\Department\\2011.4.103.002"
         }
      ]

   }
4

1 回答 1

6

你可以这样做:

data =   {
      "BuildId":"4c53575f-36f48a7f1f37",
      "EventId":1,
      "ArchiveLocations":
      [
         {
            "archive_server":"CA",
            "ftp":"ftp://ABC1.com/2011_V2/2011.4.103.002",
            "dfs":"\\\\ABD\\dfs\\Department\\2011.4.103.002"
         },
         {
            "archive_server":"CT",
            "ftp":"ftp://ABC2.com/2011_V2/2011.4.103.002",
            "dfs":"\\\\ABD.com\\dfs\\Department\\2011.4.103.002"
         }
      ]

   }

buildid = data['BuildId']
firstserver = data['ArchiveLocations'][0]['archive_server']
secondserver = data['ArchiveLocations'][1]['archive_server']

print buildid, secondserver

这返回4c53575f-36f48a7f1f37 CT

或者,您可以遍历每个键并以这种方式提取值。很难给你一个完整的答案,因为我不知道你的问题的上下文。

于 2013-02-07T21:37:39.833 回答