如何从给定的 json 文件生成自己的 json 解析器?



我一直在尝试从给定的 json 文件中生成自己的 json 解析器。给定的 json 如下所示:

{
"id": "Z3PvTW",
"title": "TESTING",
"theme": {
"id": "xwizbR",
"font": "Oswald",
"name": "Plain Blue (copy)",
"colors": {
"question": "#3D3D3D",
"answer": "#000000",
"button": "#000000",
"background": "#FFFFFF"
},
"has_transparent_button": false,
"visibility": "private"
},
"workspace": {
"href": "https:test"
},
"settings": {
"is_public": true,
"is_trial": false,
"language": "en",
"progress_bar": "proportion",
"show_progress_bar": true,
"show_typeform_branding": true,
"meta": {
"allow_indexing": false
}
},
"welcome_screens": [{
"ref": "a13820db-af60-40eb-823d-86cf0f20299b",
"title": "yes",
"properties": {
"show_button": true,
"button_text": "Start"
}
}],
"thankyou_screens": [{
"ref": "default_tys",
"title": "Done! Your information was sent perfectly.",
"properties": {
"show_button": false,
"share_icons": false
}
}],
"fields": [{
"id": "kxWycKljdtBq",
"title": "FIRST NAME",
"ref": "27f403f7-8c5b-4e18-b19d-1501e8f137ee",
"validations": {
"required": true
},
"type": "short_text"
}, {
"id": "WEXCnZ7EAFjN",
"title": "LAST NAME",
"ref": "a6bf6d83-ee37-4870-b6c5-779822290cde",
"validations": {
"required": true
},
"type": "short_text"
}, {
"id": "ButwoV1bTge5",
"title": "EMAIL ADDRESS",
"ref": "8860a4cf-71ec-4bfa-a2c7-934fd405f200",
"properties": {
"description": "hehe"
},
"validations": {
"required": true
},
"type": "email"
}, {
"id": "kzz9Bph353rg",
"title": "ADDRESS",
"ref": "e65a4c34-fd2e-4d47-b546-ac2d70679004",
"validations": {
"required": true
},
"type": "short_text"
}, {
"id": "AzZsa4HT2g7g",
"title": "ADDRESS LINE 2",
"ref": "35a7c7eb-1617-45a4-b5fa-36b4c6dabfb6",
"validations": {
"required": false
},
"type": "short_text"
}, {
"id": "u5EKtgbNramz",
"title": "POSTALu002FZIP CODE",
"ref": "a9bb3c05-0c86-4efb-85c1-7e3a4a42f3ec",
"validations": {
"required": true
},
"type": "short_text"
}, {
"id": "q1AIcLze6SdV",
"title": "CITY",
"ref": "aead9286-0dff-42f2-8e66-95fffe8711ab",
"validations": {
"required": true
},
"type": "short_text"
}, {
"id": "Dazspa7NoUI1",
"title": "STATEu002FPROVINCEu002FREGION",
"ref": "eefcc10a-ad87-4f73-be2a-fb286eb5be06",
"validations": {
"required": false
},
"type": "short_text"
}, {
"id": "u26XWl568uQI",
"title": "COUNTRY",
"ref": "98ba3e50-c1e2-424f-9487-2c25c7eccaba",
"properties": {
"alphabetical_order": false,
"randomize": false,
"choices": [{
"label": "Afghanistan"
}, {
"label": "Albania"
}, {
"label": "Cambodia"
}, {
"label": "Sweden"
}]
},
"validations": {
"required": true
},
"type": "dropdown"
}, {
"id": "q9PZyyjeRrGx",
"title": "Fruit",
"ref": "805ec00a-b179-4fcb-9ebb-651409ea6751",
"properties": {
"alphabetical_order": false,
"randomize": false,
"choices": [{
"label": "Apple"
}, {
"label": "Penut"
}]
},
"validations": {
"required": false
},
"type": "dropdown"
}],
"_links": {
"display": "https:test"
}
}

这很容易"抓取",但是我确实想将其转换为拥有自己的 json 解析器,该解析器应该是使用上述值的输出:

{
"signature": "1234567" #random numbers,
"form_id": "Z3PvTW",
"landed_at": 1580244308 #epoch time,
"answers": [
{
"field": {
"id": "kxWycKljdtBq", #From the first fields list
"type": "short_text"
},
"type": "text",
"text": #will create own config.json file to add a value here
},
{
"field": {
"id": "WEXCnZ7EAFjN",
"type": "short_text"
},
"type": "text",
"text": #will create own config.json file to add a value here
},
{
"field": {
"id": "ButwoV1bTge5",
"type": "email"
},
"type": "email",
"email": #will create own config.json file to add a value here
},
{
"field": {
"id": "kzz9Bph353rg",
"type": "short_text"
},
"type": "text",
"text": #will create own config.json file to add a value here
},
{
"field": {
"id": "AzZsa4HT2g7g",
"type": "short_text"
},
"type": "text",
"text": #will create own config.json file to add a value here
},
{
"field": {
"id": "u5EKtgbNramz",
"type": "short_text"
},
"type": "text",
"text": #will create own config.json file to add a value here
},
{
"field": {
"id": "q1AIcLze6SdV",
"type": "short_text"
},
"type": "text",
"text": #will create own config.json file to add a value here
},
{
"field": {
"id": "u26XWl568uQI",
"type": "dropdown"
},
"type": "text",
"text": #will create own config.json file to add a value here
},
{
"field": {
"id": "q9PZyyjeRrGx",
"type": "dropdown"
},
"type": "text",
"text": #will create own config.json file to add a value here
}
]
}

所以我的想法是做一些事情,比如有一个空的列表,我使用 append(( 函数在其中附加内容:

testList = []
for test in json.loads(data).get('fields'): #data is the first given json as I posted at top
testList["answers"].append({'id':'{}'.format(test.get('id'))})

但我立即收到一个错误说:

testList["answers"].append({'id':'{}'.format(test.get('id'))})
TypeError: list indices must be integers or slices, not str

所以我不太确定我是否以正确的方式做,或者是否有更简单的方法可以做到这一点,然后我想做什么?

我将不胜感激有关如何将第一个 json 转换为第二个 json 的所有帮助

好吧,这里的错误是明确的。由于您将 testList 定义为列表,因此字符串索引不适用于它。

一种可能的解决方案是使用字典。例如:

with open('yourfile.json') as file:
data = json.load(file)
testList = {"answers":[]}
for test in data['fields']: #data is the first given json as I posted at top
testList["answers"].append({'id':'{}'.format(test.get('id'))})

这会将与密钥answers关联的列表放入testList

相关内容

最新更新