. Advertisement .
..3..
. Advertisement .
..4..
I got the error message: string indices must be integers python json. However, I don’t know the reason and solution of that problem. Can someone help me, please? Thanks.
The concept detail in the example below:
import json
input_string = '{"coord": {"lon": 4.49, "lat": 50.73}, "weather": [{"id": 800, "main": "Clear", "description": "clear sky", "icon": "01d"}], "base": "stations", "main": {"temp": 26.96, "feels_like": 23.13, "temp_min": 26.11, "temp_max": 27.78, "pressure": 1016, "humidity": 26}, "visibility": 10000, "clouds": {"all": 0}, "dt": 1596629272, "timezone": 7200, "id": 2793548, "name": "La Hulpe", "cod": 200}'
data = json.loads(input_string)
for i in data['weather']:
print(i['main'])
for j in data['main']:
print(j['temp'])
Then the output shows that error.
The cause: If the value inside the brackets is not an integer type, the error “string indices must be integers” will appear.
Solution: Passing the index value as the integer value is the only way to get around the “String indices must be integers” problem. Because only the integer value may be used to access the iterable object.
Example:
Output: n