Java 如何从Youtube Api获取频道Id?它显示了这个错误;JSONException:channelId没有值;
我正在使用Youtube Api获取频道Id,但我得到了那个错误 我读了这个答案 但它不能解决我的问题 这是一些JSON代码Java 如何从Youtube Api获取频道Id?它显示了这个错误;JSONException:channelId没有值;,java,android,arrays,json,Java,Android,Arrays,Json,我正在使用Youtube Api获取频道Id,但我得到了那个错误 我读了这个答案 但它不能解决我的问题 这是一些JSON代码 { "kind":"youtube#activityListResponse", "etag":"\"8jEFfXBrqiSrcF6Ee7MQuz8XuAM/OMMhc8F-t0SORJfXv_Owv4A3N_g\"", "nextPageToken":"CAUQAA", "pageInfo":{ "totalResult
{
"kind":"youtube#activityListResponse",
"etag":"\"8jEFfXBrqiSrcF6Ee7MQuz8XuAM/OMMhc8F-t0SORJfXv_Owv4A3N_g\"",
"nextPageToken":"CAUQAA",
"pageInfo":{
"totalResults":20,
"resultsPerPage":5
},
"items":[
{
"kind":"youtube#activity",
"etag":"\"8jEFfXBrqiSrcF6Ee7MQuz8XuAM/WUtPyfC60sUnCViYyIHtr7rKu5E\"",
"id":"VTE1NjcxMTI0MzYxNDAyNDEzMTcwNzczOTI=",
"snippet":{
"publishedAt":"2019-08-29T21:00:36.000Z",
"channelId":"UC_x5XG1OV2P6uZZ5FSM9Ttw",
"title":"Android Studio 3.5, Cloud Run Button, BigQuery Terraform module",
这是java代码
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonObject = new JSONObject(s);
Log.i("jsonObject ", jsonObject.toString());
String items= jsonObject.getString("items");
JSONArray arr = new JSONArray(items);
for (int i=0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
Log.i("ID",jsonPart.getString("channelId"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
但是它显示了这个错误“JSONException:No value for channelId”您想要的
JSONArray
具有项的名称,而不是片段的名称
你必须这样做:
String items = jsonObject.getString("items");
JSONArray arr = new JSONArray(items);
for (int i=0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
JSONObject snippet = new JSONObject(jsonPart.getString("snippet"))
Log.i("ID",snippet.getString("channelId"));
}
stringitems=jsonObject.getString(“items”);
JSONArray arr=新JSONArray(项目);
对于(int i=0;i
很抱歉,我这样做了,但问题仍然是存在再次显示此错误的“JSONException:No value for snippets”是的,因为它是snippet
,而不是snippets
String items = jsonObject.getString("items");
JSONArray arr = new JSONArray(items);
for (int i=0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
JSONObject snippet = new JSONObject(jsonPart.getString("snippet"))
Log.i("ID",snippet.getString("channelId"));
}