[英]How to remove JSON data array (and keep its content) in Python
I am programming a tool with the Roblox API. My code to request data from Roblox's API is below, it stores it and uses it later (not related to this issue)我正在使用 Roblox API 编写一个工具。我从 Roblox 的 API 请求数据的代码如下,它存储并稍后使用(与此问题无关)
import requests
from requests.exceptions import HTTPError
import json
try:
response = requests.get('https://groups.roblox.com/v2/groups?groupIds=10491230') # example random group
response.raise_for_status()
# access JSOn content
json1 = response.json()
print(json1)
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
The output is below (value of json1)下面是 output(json1 的值)
{
"data": [
{
"id": 0,
"name": "string",
"description": "string",
"owner": {
"id": 0,
"type": "User",
"name": "string"
},
"memberCount": 0,
"created": "2022-05-09T16:16:38.105Z"
}
]
}
Since I need to parse and interact with the data in the data[] array, I need to remove it, so the output would be simular to the output below.由于我需要解析 data[] 数组中的数据并与之交互,因此我需要将其删除,因此 output 将类似于下面的 output。
{
"id": 0,
"name": "string",
"description": "string",
"owner": {
"id": 0,
"type": "User",
"name": "string"
},
"memberCount": 0,
"created": "2022-05-09T16:16:38.105Z"
}
How would I remove the data[] part, but keep the data inside?我将如何删除 data[] 部分,但将数据保留在里面? I tried replace() and JSON parsing, but have no idea how to start.我试过 replace() 和 JSON 解析,但不知道如何开始。
Language: Python语言:Python
Thanks!谢谢!
API documentation is available at https://groups.roblox.com/docs#!/Groups/get_v2_groups API 文档位于https://groups.roblox.com/docs#!/Groups/get_v2_groups
This should work这应该工作
json1 = response.json()["data"]
You can store content inside data array in another variable您可以将内容存储在数据数组中的另一个变量中
data = json1["data"][0]
or if you want the whole array或者如果你想要整个数组
data = json1["data"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.