JSON解析Python

2024-07-03 08:11:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个来自VK的json数据。在

 {
"response": [{
    "id": 156603484,
    "name": "Equestria in the Space",
    "screen_name": "equestriaspace",
    "is_closed": 0,
    "type": "group",
    "is_admin": 1,
    "admin_level": 3,
    "is_member": 1,
    "description": "Официально сообщество Equestria in the Space!",
    "photo_50": "https://pp.userap...089/u0_mBSE4E34.jpg",
    "photo_100": "https://pp.userap...088/O6vENP0IW_w.jpg",
    "photo_200": "https://pp.userap...086/rwntMz6YwWM.jpg"
    }]
}

所以我只想打印“姓名”,但当我这样做时,它给了我一个错误

TypeError: list indices must be integers or slices, not str

我的代码是:

^{pr2}$

你知道我怎么修吗?在google中,我发现了如何用一个数组解析json。但这里有两个或更多

p.S不怎么打败我。我是Python新手,只是在学习


Tags: the数据nameinhttpsjsonadminis
1条回答
网友
1楼 · 发布于 2024-07-03 08:11:13

response的值是什么样的数据结构?在

也就是说,如果我给你以下这些,你怎么会得到它?在

"response": [{
  "id": 156603484,
  "name": "Equestria in the Space",
  "screen_name": "equestriaspace",
  "is_closed": 0,
  "type": "group",
  "is_admin": 1,
  "admin_level": 3,
  "is_member": 1,
  "description": "Официально сообщество Equestria in the Space!",
  "photo_50": "https://pp.userap...089/u0_mBSE4E34.jpg",
  "photo_100": "https://pp.userap...088/O6vENP0IW_w.jpg",
  "photo_200": "https://pp.userap...086/rwntMz6YwWM.jpg"
},
{
  "not_a_real_response": "just some garbage actually"
}]

您需要在该响应数组中选择第一个响应。正如评论中的好人已经告诉你的那样。在

name = result['response'][0]['name']

相关问题 更多 >