![](/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.