我的代码如下:JSON文件是https://www.dropbox.com/s/uwqfqb27blxr1bj/citiesclimate_2014_03_27.json
小样本:
[ { "_id" : { "$oid" : "5333d7e18828169279d9250d" },
"actions" : null,
"actions_nr" : 0,
"city" : "Adachi City",
"citylink" : "<a href=\"/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198\" target=\"_blank\" >Adachi City</a>",
"commitments" : "338,339",
"commitments_nr" : 1,
"country" : "Japan",
"latitude" : "35.465",
"longitude" : "139.482",
"performance" : "355,356,1090,1091",
"performance_nr" : 4,
"uid" : "198"
},
{ "_id" : { "$oid" : "5333d7e18828169279d92511" },
"test" : [ { "actions" : null,
"actions_nr" : 0,
"city" : "Adachi City",
"citylink" : "<a href=\"/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198\" target=\"_blank\" >Adachi City</a>",
"commitments" : "338,339",
"commitments_nr" : 1,
"country" : "Japan",
"latitude" : "35.465",
"longitude" : "139.482",
"performance" : "355,356,1090,1091",
"performance_nr" : 4,
"uid" : "198"
},
{ "actions" : "3025,3105,3106,3108,3109,3110,3111,3112,3113,3114,3115,3116,3164,3166,3167,3168,3170,3171,3172,3173,3174,3175,3176,3177,3180,3181,3182,3183,3184,3185,3187,3188,3189,3190,3191,3192,3193,3194,3196,3197,3410",
"actions_nr" : 41,
"city" : "Ahmadabad City",
"citylink" : "<a href=\"/data/report/commitments/?tx_datareport_pi1%5Buid%5D=549\" target=\"_blank\" >Ahmadabad City</a>",
"commitments" : "816",
"commitments_nr" : 1,
"country" : "India",
"latitude" : "23.0300",
"longitude" : "72.5800",
"performance" : "900,901",
"performance_nr" : 2,
"uid" : "549"
}
]
}
]
我一直得到字符串索引必须是整数
json_file = source_json
with open(json_file) as json_file:
json_data = json.load(json_file)
for e in json_data: # iterator over a dictionary
#print e
for key, value in e.iteritems():
if key != '_id':
print key, value
#city_climate_data['city'] = value['test.city']
#print city_climate_data['city']
答案 0 :(得分:1)
我一直得到字符串索引必须是整数
在JSON中,一对必须为string : value
。不能将整数设为key
。
修改强>
我在你的问题中添加了一小部分JSON样本。使用此JSON,您的代码将无法工作,因为字典没有iteritems
。你可以这样做:
for e in json_data:
for k, v in e.items():
if k != '_id':
print k, v
这会给出这个输出:
country, Japan
citylink, <a href="/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198" target="_blank" >Adachi City</a>
commitments, 338,339
commitments_nr, 1
longitude, 139.482
performance_nr, 4
actions_nr, 0
latitude, 35.465
uid, 198
actions, None
performance, 355,356,1090,1091
city, Adachi City
test, [{'country': 'Japan', 'commitments': '338,339', 'citylink': '<a href="/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198" target="_blank" >Adachi City</a>', 'commitments_nr': 1, 'longitude': '139.482', 'performance_nr': 4, 'actions_nr': 0, 'latitude': '35.465', 'uid': '198', 'actions': None, 'performance': '355,356,1090,1091', 'city': 'Adachi City'}, {'country': 'India', 'commitments': '816', 'citylink': '<a href="/data/report/commitments/?tx_datareport_pi1%5Buid%5D=549" target="_blank" >Ahmadabad City</a>', 'commitments_nr': 1, 'longitude': '72.5800', 'performance_nr': 2, 'actions_nr': 41, 'latitude': '23.0300', 'uid': '549', 'actions': '3025,3105,3106,3108,3109,3110,3111,3112,3113,3114,3115,3116,3164,3166,3167,3168,3170,3171,3172,3173,3174,3175,3176,3177,3180,3181,3182,3183,3184,3185,3187,3188,3189,3190,3191,3192,3193,3194,3196,3197,3410', 'performance': '900,901', 'city': 'Ahmadabad City'}]
您对JSON的哪个部分感兴趣?