我有这个json api:https://api.glive.live/h5/v1/live_room/home?&page_size=1000
如何打印所有“ anchor_id”的?
import requests
api = "https://api.glive.live/h5/v1/live_room/home?"
url = api + '&page_size=1000'
raw_json_data = requests.get(url).json()
data = raw_json_data['rooms']['anchor_id']
print(data)
这给了我输出:
data = raw_json_data['rooms']['anchor_id']
TypeError: list indices must be integers or slices, not str
然后我尝试做:
data = raw_json_data['rooms'](str['anchor_id'])
并获得输出:
data = raw_json_data['rooms'](str['anchor_id'])
TypeError: 'type' object is not subscriptable
答案 0 :(得分:1)
出现错误是因为final TextView averageView = findViewById(R.id.averageView);
final String averageText = getString(R.string.average);
final Button calculateButton = findViewById(R.id.calculateAverageButton);
final Button addGradeButton = findViewById(R.id.addGradeButton)
calaculateButton.setOnClickListener(new View.onClickListener() {
@SupressLint("SetTextI18n")
public void onClick(View v) {
double grade[] = {Double.parseDouble(((EditText) findViewById(R.id.grade1)).getText().toString());
double weight[] = {Double.parseDouble(((EditText) findViewById(R.id.weight1)).getText().toString());
double weightTotal = weight[0]; double sum = grade[0] * weight[0]
double average = sum / weightTotal
@SuppressLint("DefaultLocale") String averageResult = String.format(%.2f, average);
averageView.setText(averageText + " " + averageResult);
返回了一个字典列表,而用字符串(raw_json_data['rooms']
)索引到列表中却没有原始错误所暗示的意义。
由于多种原因,您尝试解决它的方法(['anchor_id']
)没有任何意义,最明显的一个事实是raw_json_data['rooms'](str['anchor_id'])
是一种类型,因此str
是str[...]
。
由于TypeError
返回一个列表,因此您必须使用循环对其进行迭代:
raw_json_data['rooms']