在处理不以数字格式列出的JSON API数据时,可以使用以下代码示例中的一种解决方法:
Object.keys()
方法获取JSON对象的所有属性,然后遍历这些属性并访问其值。fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
Object.keys(data).forEach(key => {
const value = data[key];
console.log(`Key: ${key}, Value: ${value}`);
});
})
.catch(error => console.error(error));
json
库将JSON字符串解析为字典对象,然后使用字典的items()
方法遍历所有键和值。import json
import requests
response = requests.get('https://api.example.com/data')
data = json.loads(response.text)
for key, value in data.items():
print(f"Key: {key}, Value: {value}")
请注意,这些代码示例假设你已经通过某种方式获取了JSON数据,并将其存储在变量data
中。你需要将示例代码中的URL https://api.example.com/data
替换为你实际的API地址。