0

假设我有以下 JSON 对象:

{
  "type": "Bands",
  "Artists": [{
    "profile": {
      "name":"Marissa"
      "age": "7"
      "hobbies": [{
        "name":"Skiing",
        "experience": "1 year"
      }]
    },
    "popularsong":{
      "name":"Wishing on a Star"
      "year": 2007
    }
  },
{
    "profile": {
      "name":"Kelsey"
      "age": "7"
      "hobbies": [{
        "name":"Piano",
        "experience": "1 year"
      }]
    }
    "popularsong":{
      "name":"The Twinkle"
      "year": 2007
    }
  },...
  ]
}

假设这个 json 被加载到字典json.loads(json_string) 中,搜索所有“年龄”为“7”的“艺术家”条目并将它们粘贴到数组或另一个字典中的最有效方法是什么?

请注意,艺术家条目由“个人资料”和“流行歌曲”组成,例如:

{“个人资料”:{“姓名”:“玛丽莎”“年龄”:“7”“爱好”:[{“姓名”:“滑雪”,“经验”:“1年”},“流行歌曲”:{“名称”:“闪烁”“年份”:2007 }

4

1 回答 1

1

[artist for artist in myJson['Artists'] if artist['profile']['age'] == 7]如果我在精神上正确地解析了那个 JSON,可能会起作用。

于 2012-08-13T23:44:14.760 回答