简体   繁体   English

python解析json文件

[英]python to parse json file

i am new to python and json. 我是python和json的新手。 I have a below json file in which i need to parse the "value" from the json file 我有一个下面的json文件,我需要在其中解析json文件中的“值”

{
  "link": [
    {
      "attributes": [
        {
          "value": "backup",
          "name": "name"
        },
        {
          "value": "",
          "name": "description"
        }
      ],

    },
    {
      "attributes": [
        {
          "value": "com.cap.blueprints",
          "name": "name"
        },
        {
          "value": "",
          "name": "description"
        }
      ],

    }
  ],
}

i have tried the below code. 我试过下面的代码。 but i am getting error 但我遇到错误

with open ("respose_json.txt") as f2:
 data=json.load(f2)
 for x in data:
  print(x['attributes']['value']) 

error:
    print(x['attributes']['value']) 
TypeError: string indices must be integers
  1. You're missing the outermost link key. 您缺少最外面的link键。
  2. Attributes contain list of attribute and each attribute have value. 属性包含属性列表,每个属性都有值。

Try something like this: 尝试这样的事情:

import json

with open("respose_json.txt") as json_file:
    data = json.loads(json_file)["link"]

    for attributes in data:
        for attribute in attributes['attributes']:
            print(attribute['value'])

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

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