在JSON文件中的Python中更新键值

时间:2019-05-01 22:41:30

标签: python json

如何使用python更改json文件中的值?我想搜索并找到"class": "DepictionScreenshotsView"并将其替换为"class": ""

JSON文件:

{
   "minVersion": "0.1",
   "class": "DepictionTabView",
   "tintColor": "#2cb1be",
   "headerImage": "",
   "tabs": [
      {
         "tabname": "Details",
         "class": "DepictionStackView",
         "tintColor": "#2cb1be",
         "views": [
            {
               "class": "DepictionSubheaderView",
               "useBoldText": true,
               "useBottomMargin": false,
               "title": "Description"
            },
            {
               "class": "DepictionMarkdownView",
               "markdown": "Some dummy text...",
               "useRawFormat": true
            },
            {
               "class": "DepictionSeparatorView"
            },
            {
               "class": "DepictionSubheaderView",
               "useBoldText": true,
               "useBottomMargin": false,
               "title": "Screenshots"
            },
            {
               "class": "DepictionScreenshotsView",
               "itemCornerRadius": 6,
               "itemSize": "{160, 284.44444444444}",
               "screenshots": [
                  {
                     "accessibilityText": "Screenshot",
                     "url": "http://example.com/image.png"
                  }
               ]
            },
            {
               "class": "DepictionSeparatorView"
            },
            {
               "class": "DepictionSubheaderView",
               "useBoldText": true,
               "useBottomMargin": false,
               "title": "Information"
            },
            {
               "class": "DepictionTableTextView",
               "title": "Author",
               "text": "User"
            },
            {
               "class": "DepictionSpacerView",
               "spacing": 16
            },
            {
               "class": "DepictionStackView",
               "views": [
                  {
                     "class": "DepictionTableButtonView",
                     "title": "Contact",
                     "action": "http://example.com/",
                     "openExternal": true
                  }
               ]
            },
            {
               "class": "DepictionSpacerView",
               "spacing": 16
            }
         ]
      },
      {
         "tabname": "History",
         "class": "DepictionStackView",
         "views": [
            {
               "class": "DepictionSubheaderView",
               "useBoldText": true,
               "useBottomMargin": false,
               "title": ""
            },
            {
               "class": "DepictionMarkdownView",
               "markdown": "<ul>\n<li>Initial release.<\/li>\n<\/ul>",
               "useRawFormat": true
            }
         ]
      }
   ]
}

1 个答案:

答案 0 :(得分:0)

您可以按照以下方式在python中读取json文件:

import json
with open('your_file.json') as f:
    data = json.load(f)

然后您访问并更改值(针对您的情况):

data['tabs'][0]['views'][4]['class'] = ""

更改数据后,可以保存它:

with open('your_file.json', 'w') as outfile:
    json.dump(data, outfile)