0
我讀的蟒蛇以下JSON文件:迭代通過JSON [Python的]
{
"name": "Property",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"uuid": {
"type": "string"
},
"userID": {
"type": "number"
},
"address": {
"type": "string"
},
"price": {
"type": "number"
},
"lastUpdated": {
"type": "string"
}
},
"validations": [],
"relations": {
"rooms": {
"type": "hasMany",
"model": "Room",
"foreignKey": "id"
},
"addedByUser": {
"type": "hasMany",
"model": "User_ESPC",
"foreignKey": "id"
}
},
"acls": [],
"methods": {}
}
我想讀的properties
並獲得屬性的名稱(如的「uuid」),併爲每個名稱我想讀取對象的類型。到目前爲止,我的代碼列出了所有類似的特性:
Property name: price
Property name: userID
Property name: uuid
Property name: lastUpdated
Property name: address
,做這是該代碼:
import json
#json_file='a.json'
json_file='common/models/property.json'
with open(json_file, 'r') as json_data:
data = json.load(json_data)
propertyName = data["name"]
properties = data["properties"]
# print (properties)
for property in properties:
print ('Property name: ' + property)
# propertyType = property["type"]
# print (propertyType)
問題是,當我去掉底部的2線,應該得到的類型屬性對象我得到一個錯誤:
Property name: price
Traceback (most recent call last):
File "exportPropertyToAndroid.py", line 19, in <module>
propertyType = property["type"]
TypeError: string indices must be integers
謝謝你的答案。我明白我做錯了什麼!非常感謝你! :) –