【问题标题】:How to get value access to an item in an array in json file如何获取对json文件中数组中项目的值访问
【发布时间】:2021-06-08 09:51:43
【问题描述】:

我正在尝试处理一个巨大的 json 文件,其格式如下所示。如下所示,它有一个名为features 的键,它是一个元素数组,如图所示。我想知道如何从数组features 中获取子值。例如,我想访问和处理位于features 中的segments,它也是一个数组。

更新

我从一个函数返回dataAsJSONObject["features"][0]['properties']['segments'][0]['distance'],但它会产生以下错误。

错误

raise TypeError(
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a float.
127.0.0.1 - - [10/Mar/2021 14:15:17] "[35m[1mGET /getDistance/31.205753,29.924526,8.687872,49.420318 HTTP/1.1[0m" 500 -

代码

print('dataAsJSONObject[features]: %s' %
(dataAsJSONObject['features']))

结果

[features]: [{'bbox': [29.781908, 8.397103, 49.420574, 31.205812], 'type': 'Feature', 'properties': {'segments': [{'distance': 4637246.1, 'duration': 232856.8, 'steps': [{'distance': 16.8, 'duration': 4.0, 'type': 11, 'instruction': 'Head southwest', 'name': '-', 'way_points': [0, 1]}, {'distance': 52.1, 'duration': 12.5, 'type': 0, 'instruction': 'Turn left', 'name': '-', 'way_points': [1, 3]}, {'distance': 1099.0, 'duration': 65.9, 'type': 1, 'instruction': 'Turn right onto شارع جمال عبد الناصر', 'name': 'شارع جمال عبد الناصر', 'way_points': [3, 28]}, {'distance': 140.2, 'duration': 8.4, 'type': 4, 'instruction': 'Turn slight left onto شارع جمال عبد الناصر', 'name': 'شارع جمال عبد الناصر', 'way_points': [28, 34]}, {'distance': 328.6, 'duration': 21.5, 'type': 13, 'instruction': 'Keep right onto شارع عبد الله على حسن', 'name': 'شارع عبد الله على حسن', 'way_points': [34, 43]}, {'distance': 129.0, 'duration': 13.3, 'type': 12, 'instruction': 'Keep left', 'name': '-', 'way_points': [43, 50]}, {'distance': 201954.1, 'duration': 6832.3, 'type': 12, 'instruction': 'Keep left', 'name': '-', 'way_points': [50, 907]}, {'distance': 8351.0, 'duration': 360.0, 'type': 13, 'instruction': 'Keep right onto محور صحاري الأهرام', 'name': 'محور صحاري الأهرام', 'way_points': [907, 1022]}, {'distance': 19937.6, 'duration': 905.0, 'type': 13, 'instruction': 'Keep right', 'name': '-', 'way_points': [1022, 1223]}, {'distance': 394.7, 'duration': 17.8, 'type': 6, 'instruction': 'Continue straight onto الطريق الدائري, 516', 'name': 'الطريق الدائري, 516', 'way_points': [1223, 1233]}, {'distance': 3375.9, 'duration': 151.9, 'type': 13, 'instruction': 'Keep right onto الطريق الدائري, 516', 'name': 'الطريق الدائري, 516', 'way_points': [1233, 1275]}, {'distance': 677.5, 'duration': 47.4, 'type': 13, 'instruction': 'Keep right', 'name': '-', 'way_points': [1275, 1292]}, {'distance': 98262.6, 'duration': 3216.1, 'type': 13, 'instruction': 'Keep right onto طريق القطامية, العين السخنة, 176', 'name': 'طريق القطامية, العين السخنة, 176', 'way_points': [1292, 1682]}, {'distance': 95608.2, 'duration': 3199.6, 'type': 13, 'instruction': 'Keep right', 'name': '-', 'way_points': [1682, 2185]}, {'distance': 229986.0, 'duration': 8492.9, 'type': 6, 'instruction': 'Continue straight onto طريق الزعفرانه, رأس غارب, 11', 'name': 'طريق الزعفرانه, رأس غارب, 11', 'way_points': [2185, 2959]}, {'distance': 31123.9, 'duration': 1318.2, 'type': 13, 'instruction': 'Keep right onto طريق راس غارب, الغردقة, 11', 'name': 'طريق راس غارب, الغردقة, 11', 'way_points': [2959, 3098]}, {'distance': 31983.1, 'duration': 1152.3, 'type': 7, 'instruction': 'Enter the roundabout and take the 1st exit onto طريق الغردقة, سفاجا, 11', 'name': 'طريق الغردقة, سفاجا, 11', 'exit_number': 1, 'way_points': [3098, 3319]}, {'distance': 50184.6, 'duration': 2233.7, 'type': 7, 'instruction': 'Enter the roundabout and take the 1st exit onto طريق الغردقة, سفاجا, 11', 'name': 'طريق الغردقة, سفاجا, 11', 'exit_number': 1, 'way_points': [3319, 3674]}, {'distance': 34459.4, 'duration': 1460.2, 'type': 12, 'instruction': 'Keep left onto طريق سفاجا, القصير, 11', 'name': 'طريق سفاجا, القصير, 11', 'way_points': [3674, 3869]}, {'distance': 842.4, 'duration': 35.7, 'type': 6, 'instruction': 'Continue straight onto طريق سفاجا, القصير, 11', 'name': 'طريق سفاجا, القصير, 11', 'way_points': [3869, 3876]}, {'distance': 8483.1, 'duration': 359.3, 'type': 5, 'instruction': 'Turn slight right onto طريق سفاجا, القصير', 'name': 'طريق سفاجا, القصير', 'way_points': [3876, 4009]}, {'distance': 723.0, 'duration': 30.6, 'type': 7, 'instruction': 'Enter the roundabout and take the 2nd exit onto طريق القصير, مرسى علم', 'name': 'طريق القصير, مرسى علم', 'exit_number': 2, 'way_points': [4009, 4045]}, {'distance': 244.8, 'duration': 10.4, 'type': 7, 'instruction': 'Enter the roundabout and take the 2nd exit', 'name': '-', 'exit_number': 2, 'way_points': [4045, 4065]}, {'distance': 7050.6, 'duration': 298.6, 'type': 7, 'instruction': 'Enter the roundabout and take the 2nd exit', 'name': '-', 'exit_number': 2, 'way_points': [4065, 4143]}, {'distance': 64665.3, 'duration': 2738.7, 'type': 6, 'instruction': 'Continue straight onto طريق القصير, مرسى علم, 11', 'name': 'طريق القصير, مرسى علم, 11', 'way_points': [4143, 4565]}, {'distance': 2916.5, 'duration': 123.5, 'type': 7, 'instruction': 'Enter the roundabout and take the 2nd exit onto طريق القصير, مرسى علم, 11', 'name': 'طريق القصير, مرسى علم, 11', 'exit_number': 2, 'way_points': [4565, 4596]}, {'distance': 6229.3, 'duration': 263.8, 'type': 7, 'instruction': 'Enter the roundabout and take the 2nd exit onto طريق القصير, مرسى علم, 11', 'name': 'طريق القصير, مرسى علم, 11', 'exit_number': 2, 'way_points': [4596, 4633]}, {'distance': 18844.5, 'duration': 798.1, 'type': 6, 'instruction': 'Continue straight onto طريق القصير, مرسى علم, 11', 'name': 'طريق القصير, مرسى علم, 11', 'way_points': [4633, 4746]}, {'distance': 53367.8, 'duration': 2260.2, 'type': 7, 'instruction': 'Enter the roundabout and take the 2nd exit onto طريق القصير, مرسى علم, 11', 'name': 'طريق القصير, مرسى علم, 11', 'exit_number': 2, 'way_points': [4746, 5320]}, {'distance': 392436.4, 'duration': 16620.8, 'type': 5, 'instruction': 'Turn slight right onto طريق حلايب وشلاتين, 11', 'name': 'طريق حلايب وشلاتين, 11', 'way_points': [5320, 7146]}, {'distance': 1546.0, 'duration': 97.8, 'type': 2, 'instruction': 'Turn sharp left', 'name': '-', 'way_points': [7146, 7153]}, {'distance': 54871.4, 'duration': 3292.3, 'type': 13, 'instruction': 'Keep right', 'name': '-', 'way_points': [7153, 7352]}, {'distance': 84018.4, 'duration': 3558.4, 'type': 0, 'instruction': 'Turn left onto طريق حلايب وشلاتين, 11', 'name': 'طريق حلايب وشلاتين, 11', 'way_points': [7352, 7546]}, {'distance': 22394.8, 'duration': 948.5, 'type': 12, 'instruction': 'Keep left', 'name': '-', 'way_points': [7546, 7587]}, {'distance': 25064.8, 'duration': 1061.6, 'type': 13, 'instruction': 'Keep right', 'name': '-', 'way_points': [7587, 7631]}, {'distance': 47272.4, 'duration': 2002.1, 'type': 13, 'instruction': 'Keep right', 'name': '-', 'way_points': [7631, 7781]}, {'distance': 53779.2, 'duration': 2277.7, 'type': 12, 'instruction': 'Keep left', 'name': '-', 'way_points': [7781, 7847]}, {'distance': 9857.1, 'duration': 417.5, 'type': 13, 'instruction': 'Keep right', 'name': '-', 'way_points': [7847, 7864]}, {'distance': 35283.8, 'duration': 1494.4, 'type': 13, 'instruction': 'Keep right', 'name': '-', 'way_points': [7864, 7948]}, {'distance': 81.7, 'duration': 3.5, 'type': 12, 'instruction': 'Keep left', 'name': '-', 'way_points': [7948, 7950]}, {'distance': 6430.8, 'duration': 272.3, 'type': 12, 'instruction': 'Keep left', 'name': '-', 'way_points': [7950, 8098]}, {'distance': 24.1, 'duration': 1.0, 'type': 5, 'instruction': 'Turn slight right', 'name': '-', 'way_points': [8098, 8099]}, {'distance': 501.7, 'duration': 21.2, 'type': 0, 'instruction': 'Turn left', 'name': '-', 'way_points': [8099, 8110]}, {'distance': 921.7, 'duration': 39.0, 'type': 13, 'instruction': 'Keep right', 'name': '-', 'way_points': [8110, 8128]}, {'distance': 3926.1, 'duration': 166.3, 'type': 1, 'instruction': 'Turn right', 'name': '-', 'way_points': [8128, 8213]}, {'distance': 27.0, 'duration': 1.1, 'type': 12, 'instruction': 'Keep left', 'name': '-', 'way_points': [8213, 8215]}, {'distance': 1501.1, 'duration': 63.6, 'type': 7, 'instruction': 'Enter the roundabout and take the 2nd exit', 'name': '-', 'exit_number': 2, 'way_points': [8215, 8242]}, {'distance': 205672.9, 'duration': 8710.8, 'type': 12, 'instruction': 'Keep left', 'name': '-', 'way_points': [8242, 9392]}, {'distance': 42.6, 'duration': 2.3, 'type': 7, 'instruction': 'Enter the roundabout and take the 1st exit onto Kassala PortSudan Highway', 'name': 'Kassala PortSudan Highway', 'exit_number': 1, 'way_points': [9392, 9395]}, {'distance': 34.3, 'duration': 1.5, 'type': 2, 'instruction': 'Turn sharp left onto Kassala PortSudan Highway', 'name': 'Kassala PortSudan Highway', 'way_points': [9395, 9396]}, {'distance': 46.2, 'duration': 2.2, 'type': 7, 'instruction': 'Enter the roundabout and take the 1st exit onto Kassala PortSudan Highway', 'name': 'Kassala PortSudan Highway', 'exit_number': 1, 'way_points': [9396, 9398]}, {'distance': 351052.6, 'duration': 14868.0, 'type': 5, 'instruction': 'Turn slight right onto شارع بورتسودان - كسلا', 'name': 'شارع بورتسودان - كسلا', 'way_points': [9398, 10681]}, {'distance': 266.0, 'duration': 11.3, 'type': 0, 'instruction': 'Turn left onto Al Lafa Road', 'name': 'Al Lafa Road', 'way_points': [10681, 10685]}, {'distance': 40799.6, 'duration': 1728.0, 'type': 5, 'instruction': 'Turn slight right onto طريق اللفة', 'name': 'طريق اللفة', 'way_points': [10685, 10841]}, {'distance': 1877.0, 'duration': 79.5, 'type': 0, 'instruction': 'Turn left onto P-2', 'name': 'P-2', 'way_points': [10841, 10859]}, {'distance': 207.8, 'duration': 8.8, 'type': 1, 'instruction': 'Turn right onto P-2', 'name': 'P-2', 'way_points': [10859, 10862]}, {'distance': 129543.0, 'duration': 5486.4, 'type': 0, 'instruction': 'Turn left onto Teseney - Barentu Road, P-2', 'name': 'Teseney - Barentu Road, P-2', 'way_points': [10862, 11641]}, {'distance': 145705.1, 'duration': 6170.9, 'type': 5, 'instruction': 'Turn slight right onto P-2', 'name': 'P-2', 'way_points': [11641, 12815]}, {'distance': 21.5, 'duration': 0.9, 'type': 7, 'instruction': 'Enter the roundabout and take the 1st exit onto طريق أغوردات - كرن, P-2', 'name': 'طريق أغوردات - كرن, P-2', 'exit_number': 1, 'way_points': [12815, 12817]}, {'distance': 364.1, 'duration': 15.4, 'type': 6, 'instruction': 'Continue straight onto

【问题讨论】:

    标签: python arrays json python-3.x


    【解决方案1】:

    features value 是一个字典列表。每个字典都有properties 键,以字典为值。这个字典有以list 为值的键。所以这将打印一堆列表,每个列表都有段作为元素:

    for feature in dataAsJSONObject["features"]:
        for segment in feature['properties']['segments']:
            print(segment['distance'])
        
    

    【讨论】:

    • 谢谢,但我怎样才能访问数组段内的距离
    • 我编辑了我的答案,但它实际上只是访问列表或字典元素。
    • 我正在从函数返回 dataAsJSONObject["features"][0]['properties']['segments'][0]['distance'] ......但它会产生错误。 . 你能告诉我为什么它不工作
    • 顺便注意segment['steps']中也有distance。这将只返回distance 键在段中,而不是在段中。步骤
    • 请查看上面发布的更新
    猜你喜欢
    • 1970-01-01
    • 2021-09-13
    • 2020-05-06
    • 2018-12-10
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多