There is a lot of information on loading .json files, but I just cannot figure out what the problem is:
I have an external file called LocationHistory.json with various coordinates inside. For reference sake, this is how the data is listed:
{
"data" : {
"items" : [ {
"kind" : "latitude#location",
"timestampMs" : "1374870896803",
"latitude" : 34.9482949,
"longitude" : -85.3245474,
"accuracy" : 2149
}, {
"kind" : "latitude#location",
"timestampMs" : "1374870711762",
"latitude" : 34.9857898,
"longitude" : -85.3526902,
"accuracy" : 2016
}, {
"kind" : "latitude#location",
"timestampMs" : "1374870651752",
"latitude" : 34.9857898,
"longitude" : -85.3526902,
"accuracy" : 2016
}]
}
}
I'm trying to parse this information with:
import json
json_file = open ('LocationHistory.json')
json_string = json_file.read()
json_data = json.loads (json_string)
locations = json_data ["data"]
for location in locations:
print location["timestampMS"], location["latitude"], location["longitude"], location["accuracy"]
Why am I getting the error:
line 10, in
print location["timestampMS"], location["latitude"], location["longitude"], location["accuracy"]
TypeError: string indices must be integers
All the information I can find to parse .json files explains this type of solution that I have. Where am I going wrong?
Thanks in advance, I'm sure it should be a simple mistake...