简体   繁体   English

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

[英]How to generate own json parser from a given json file?

I have been trying to generate my own json parser from a json file that is given.我一直在尝试从给定的 json 文件生成我自己的 json 解析器。 The given json is looking like:给定的 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": "POSTAL\u002FZIP 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": "STATE\u002FPROVINCE\u002FREGION",
        "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"
    }
 }

which is pretty easy to "scrape" however I do want to convert it into having my own json parser that should be a output using those values above to:这很容易“抓取”,但是我确实想将它转换为拥有我自己的 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
    }
  ]
}

So my thought was to do something like having a empty List where I append stuff in it with using append() function:所以我的想法是做一些事情,比如有一个空列表,我使用 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'))})

but I immediately do get an error saying:但我立即收到一条错误消息:

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

so I am not quite sure if I am doing it the correct way or if there is anything more simplier way to do it then what I am thinking to do?所以我不太确定我是否以正确的方式做这件事,或者是否有更简单的方法来做,那么我想做什么?

I would appreciate all help on how to convert first json into the second one我将不胜感激有关如何将第一个 json 转换为第二个 json 的所有帮助

Well, the error here is explicit.嗯,这里的错误是明确的。 Since you define testList to be a list, String indexing does not work on it.由于您将 testList 定义为列表,因此字符串索引对其不起作用。

One possible solution can be using dictionaries.一种可能的解决方案是使用字典。 For example:例如:

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'))})

This would put the list associated with the key answers into testList这会将与关键answers关联的列表放入testList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM