为什么json输出这么小?

时间:2020-06-17 19:24:40

标签: python json api rest restapi

此输出应该比这里的输出更长。

我从GET请求开始,我解析一个JSON列表并提取id,然后我在第二个函数上调用它,这将给我第二个ID,然后我将使用它在第三个函数上调用。但是,我只能得到一个条目,而我应该得到更多的条目。

代码如下:

Option Explicit

Sub ListBoxTest()

    ' Worksheet
    Dim wkTest As Worksheet: Set wkTest = ThisWorkbook.Worksheets("Sheet1")

    ' Shape - no use
    Dim shTest As Shape: Set shTest = wkTest.Shapes("ListBox1")
    Debug.Print "Shape", shTest.Name, shTest.Left
    'shTest.AddItem "One" 'Run-time Error '438': Object doesn't support ...

    ' OLEObject - no use
    Dim ooTest As OLEObject: Set ooTest = wkTest.OLEObjects("ListBox1")
    Debug.Print "OLEObject", ooTest.Name, ooTest.Left
    'ooTest.AddItem "One" 'Run-time Error '438': Object doesn't support ...

    Dim obj As Object

    'Set obj = wkTest.ListBox1 ' Compile error: Method or data member not found.

    ' Object 1 Solution
    Set obj = Worksheets("Sheet1").ListBox1
    Debug.Print "Object 1", obj.Name, obj.Left
    obj.AddItem "One"

    ' Object 2 Solution
    Set obj = Sheet1.ListBox1
    Debug.Print "Object 2", obj.Name, obj.Left
    obj.AddItem "Two"

    ' Object 3 Solution
    wkTest.Activate
    Set obj = ActiveSheet.ListBox1
    Debug.Print "Object 3", obj.Name, obj.Left
    obj.AddItem "Three"

    ' Favorite
    With Sheet1.ListBox1
        Debug.Print "Favorite", .Name, .Left
        .AddItem "Four"
    End With

End Sub

Sub ListBoxClear()
    Dim obj As Object: Set obj = Worksheets("Sheet1").ListBox1
    obj.Clear
End Sub

输出如下:

from requests.auth import HTTPBasicAuth
import requests
import json
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def countries():
    data = requests.get("https://localhost:8543/api/netim/v1/countries/", verify=False, auth=HTTPBasicAuth("admin", "admin"))
    rep = data.json()
    return [elem.get("id","") for elem in rep['items']]


def regions():
    for c in countries():
        url = requests.get("https://localhost:8543/api/netim/v1/countries/{}/regions".format(c), verify=False, auth=HTTPBasicAuth("admin", "admin"))
        response = url.json()
        return [cid.get("id","") for cid in response['items']]


def city():
    for r in regions():
        api = requests.get("https://localhost:8543/api/netim/v1/regions/{}/cities".format(r), verify=False, auth=HTTPBasicAuth("admin", "admin"))
        resolt = api.json()
        return(json.dumps([{"name":r.get("name",""),"id":r.get("id", "")} for r in resolt['items']], indent=4))
city()

print(city())

我应该有一个庞大的清单,所以我不确定我缺少什么?

1 个答案:

答案 0 :(得分:1)

您需要遍历循环的所有迭代并收集结果,然后对进行json处理并返回。

data = []
for r in regions():
    api = requests.get("https://localhost:8543/api/netim/v1/regions/{}/cities".format(r), verify=False, auth=HTTPBasicAuth("admin", "admin"))
    resolt = api.json()
    data.extend([{"name":r.get("name",""),"id":r.get("id", "")} for r in resolt['items']])
return json.dumps(data, indent=4)

这将是city()的修复程序,但是所有功能都存在相同的问题。 return立即退出该函数,并且不执行其他任何操作,实际上,您所有的for循环都在执行1次迭代。

我将在此处更新示例,以使您更好地了解正在发生的事情。

您的功能基本上是这样的:

def test_fn():
    for i in [1,2,3,4]:
        return i
# output:
1
# We never see 2 or 3 or 4 because we return before looping on them.

您想要什么:

def test_fn():
    results = []
    for i in [1,2,3,4]:
        results.append(i)
    return results
# output
[1,2,3,4]

似乎您了解for循环将对列表中的每个元素执行一次操作。您不了解的是return现在结束了函数。没有更多的for循环,没有更多的动作,并且在您的代码中,您立即在for循环内返回,停止任何进一步的动作。

相关问题