简体   繁体   中英

Extracting information of JSON file

I got results through results = requests.get(url).json() results look like this:

{'type': 'FeatureCollection',  'crs': {'type': 'name', 'properties':
 {'name': 'EPSG:4326'}},  'features': [{'type': 'Feature',   
 'properties': {'kode': '0101',
     'navn': 'København',
     'region_kode': '1084.0',
     'region_navn': 'Hovedstaden'},    'bbox': [12.453042062098154,
     55.612994971371606,
     12.734252598475942,
     55.732491190632494]}]}

with results['features'] , I am getting this

[{'type': 'Feature',   'properties': {'kode': '0101',    'navn':
 'København',    'region_kode': '1084.0',    'region_navn':
 'Hovedstaden'},   'bbox': [12.453042062098154,
    55.612994971371606,
    12.734252598475942,
    55.732491190632494]}]

I want to get the information in navn and I tried all combination of

results['features']['properties']['navn']
results['features']['navn']
results['features']['properties']

they all show the same error message that: list indices must be integers or slices, not str

apparently, results['features'] is a list with a length of 1.

how can I get to navn information?

I want to make several calls as you can imagine.

The results['features'] object is a list, try results['features'][0]['properties']['navn']

Now you select the first element in the list (0), the dictionary, and from that dictionary you select the 'navn' key. The result is the value of 'navn'

Note that python lists are between [] and items are seperated by a comma and python dictionaries are between {} and consists of key, value pairs seperated by a comma.

You should try accessing the first element of the list in result['features'] , ie:

results['features'][0]['properties']['navn']

Full code:

results = {'type': 'FeatureCollection', 'crs': {'type': 'name', 'properties': {'name': 'EPSG:4326'}}, 'features': [{'type': 'Feature',
'properties': {'kode': '0101', 'navn': 'København', 'region_kode': '1084.0', 'region_navn': 'Hovedstaden'}, 'bbox': [12.453042062098154, 55.612994971371606, 12.734252598475942, 55.732491190632494]}]}
print(results['features'][0]['properties']['navn'])
# København

try this

results['features'][0]['properties']['navn']

You can try code below:

results['features'][0]['properties']['navn']
results = {'type': 'FeatureCollection', 'crs': {'type': 'name', 'properties': {'name': 'EPSG:4326'}},
       'features': [{'type': 'Feature',
                     'properties': {'kode': '0101', 'navn': 'København', 'region_kode': '1084.0',
                                    'region_navn': 'Hovedstaden'},
                     'bbox': [12.453042062098154, 55.612994971371606, 12.734252598475942, 55.732491190632494]}]}

navn = results['features'][0]['properties']['navn']
print(navn)

You got error because inside features there is one list. So, you can not get list with the help of str index and to get the properties inside the features you need to write [0] and the list will gone and you can get the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM