[英]Read the specific attribute in the json file using python
I'm writing code to convert a json model to SQLite using python. 我正在编写代码以使用python将json模型转换为SQLite。 Here is the sample json file: 这是示例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"
}
]
}
}
]
with the below code I'm able to read the foreign key constraints. 使用下面的代码,我能够读取外键约束。 But I'm struggling to read the "reference" under the SQLFK. 但是我正在努力阅读SQLFK下的“参考”。
if constraint["name"] == "ForeignKeyOne":
for field in constraint["fields"]:
fk_attribute_list.append(field["name"])
Please help me on how to read the content "reference". 请帮助我阅读内容“参考”。
"reference":{
"entityName":{
"prefix":"Rail",
"name":"ProvinceState"
},
"fields":[
{
"name":"Code"
}
]
}
You're missing a level of attribute (reference). 您缺少属性级别(参考)。 How about: 怎么样:
if constraint["name"] == "ForeignKeyOne":
for field in constraint["reference"]:
if field == 'fields':
for x in constraint["reference"][field]:
print x
x would contain {'name':'Code'} x将包含{'name':'Code'}
That's pretty static, meaning you assume the json structure you have is pretty much the same as above. 这是静态的,这意味着您假设所拥有的json结构与上面的基本相同。
The "reference" points to a dict. “参考”指向命令。 Iterating over a dict yields dict's keys. 遍历字典会产生字典的密钥。 So in your for reference in constraint["reference"]
loop, reference
will first yield the string "entityName"
then the string "fields"
. 因此,在您for reference in constraint["reference"]
循环中for reference in constraint["reference"]
, reference
将首先产生字符串"entityName"
然后产生字符串"fields"
。 You obviously understand that "somestring"["another_string"]
makes no sense, since strings are index-based (integers). 您显然理解"somestring"["another_string"]
没有意义,因为字符串是基于索引的(整数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.