![](/img/trans.png)
[英]Iterate through json file to get specific attribute values using python
[英]Read the specific attribute in the json file using python
我正在編寫代碼以使用python將json模型轉換為SQLite。 這是示例json文件:
{
"type":"MetaModel",
"entityName":{
"prefix":"Rail",
"name":"LocationProvider"
},
"attributes":[
{
"name":"abc",
"type":"string",
"maxLength":10,
"mandatory":true
}
],
"constraints":[
{
"name": "PrimaryKey",
"type": "SQLPK",
"fields": [
{
"name":"abc"
}
]
},
{
"name": "ForeignKeyOne",
"type": "SQLFK",
"fields": [
{
"name":"ab"
}
],
"reference":{
"entityName":{
"prefix":"Rail",
"name":"ProvinceState"
},
"fields":[
{
"name":"Code"
}
]
}
}
]
使用下面的代碼,我能夠讀取外鍵約束。 但是我正在努力閱讀SQLFK下的“參考”。
if constraint["name"] == "ForeignKeyOne":
for field in constraint["fields"]:
fk_attribute_list.append(field["name"])
請幫助我閱讀內容“參考”。
"reference":{
"entityName":{
"prefix":"Rail",
"name":"ProvinceState"
},
"fields":[
{
"name":"Code"
}
]
}
您缺少屬性級別(參考)。 怎么樣:
if constraint["name"] == "ForeignKeyOne":
for field in constraint["reference"]:
if field == 'fields':
for x in constraint["reference"][field]:
print x
x將包含{'name':'Code'}
這是靜態的,這意味着您假設所擁有的json結構與上面的基本相同。
“參考”指向命令。 遍歷字典會產生字典的密鑰。 因此,在您for reference in constraint["reference"]
循環中for reference in constraint["reference"]
, reference
將首先產生字符串"entityName"
然后產生字符串"fields"
。 您顯然理解"somestring"["another_string"]
沒有意義,因為字符串是基於索引的(整數)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.