【发布时间】:2017-08-12 08:47:49
【问题描述】:
我正在尝试将一些代码从 Perl 移植到 Python。该代码使用非默认标头执行 HTTP GET 请求。响应是一个 JSON 文档。顶级元素是具有名为queryResponse 的键的对象。该元素的值也是一个对象,它有一个名为entity 的键。该元素的值是一个数组。我想要那个数组中元素的数量。
这是 Perl 代码:
my ($first, $max, $count) = (0, 1000, 1000);
while ($count == $max) {
my $debugFlag = 1;
my $uri = "/webacs/api/v2/data/Devices.json?.full=true&.nocount=true&" .
".firstResult=$first&.maxResults=$max";
$piConnection->GET($uri, $headers);
my $response = decode_json $piConnection->responseContent();
$first = $first + 1000;
$count = scalar @{$response->{queryResponse}{entity}};
这是我目前在 Python 中所拥有的:
firstResult = 0
maxResults = 1000
count = 1000
while count == maxResults:
test_urn = (URL + '/webacs/api/v2/data/Devices?.full=True&.nocount=True&.maxResults=%d&.firstResult=%d') % (maxResults, firstResult)
get_response = requests.get(test_urn, verify=False)
firstResult = firstResult + 1000
count = len(get_response['queryResponse']) # This is the line I need help with
print get_response.text
我遇到问题的部分是在 JSON 响应中获取数组的长度。我该怎么做?
【问题讨论】:
-
两条建议让您获得更多回复:(1) 发布工作代码,以便我们重现您的部分结果并测试我们的解决方案(这包括定义变量,URL) ; (2) 描述你想要的功能,以便非 Perl 程序员可以提供帮助。
标签: python python-2.7 python-requests