这是我的Json,如果musicVideo id = 303或其他一些数字
,我要做的就是获取aboutAboutTheVideo数组 {
musicVideos:[
{
id:303,
youtubeVideoUrl:"https://www.youtube.com/watch?v=cvvcbcv",
prettyVideoTitle:"Pretty Title",
youtubeVideoTitle:"Crap Title",
youtubeThumbnail:"https://image.com",
thingsAboutTheVideo:[
{
id:368,
name:"Some Name",
facebook:"",
twitter:"",
instagram:"",
imdb:"",
website:"",
info:"",
image:"",
time:""
},
{
id:538,
name:"Some Name",
facebook:"",
twitter:"",
instagram:"",
imdb:"",
website:"",
info:"",
image:"",
time:""
},
{
id:539,
name:"Some Name",
facebook:"",
twitter:"",
instagram:"",
imdb:"",
website:"",
info:"",
image:"",
time:""
},
{
id:540,
name:"Some Name",
facebook:"",
twitter:"",
instagram:"",
imdb:"",
website:"",
info:"",
image:"",
time:""
}
]
},
{
id:302,
youtubeVideoUrl:"https://www.youtube.com/watch?v=",
prettyVideoTitle:"Tcbcvbcv",
youtubeVideoTitle:"xcvcx",
youtubeThumbnail:"http://www.google.com/image",
thingsAboutTheVideo:[
{
id:64,
name:"Some Name",
facebook:"",
twitter:"",
instagram:"",
imdb:"",
website:"",
info:"",
image:"",
time:""
},
{
id:535,
name:"Some Name",
facebook:"",
twitter:"",
instagram:"",
imdb:"",
website:"",
info:"",
image:"",
time:""
},
{
id:536,
name:"Some Name",
facebook:"",
twitter:"",
instagram:"",
imdb:"",
website:"",
info:"",
image:"",
time:""
},
{
id:537,
name:"Some Name",
facebook:"",
twitter:"",
instagram:"",
imdb:"",
website:"",
info:"",
image:"",
time:""
},
{
id:541,
name:"Some Name",
facebook:"",
twitter:"",
instagram:"",
imdb:"",
website:"",
info:"",
image:"",
time:""
},
{
id:542,
name:"Some Name",
facebook:"",
twitter:"",
instagram:"",
imdb:"",
website:"",
info:"",
image:"",
time:""
}
]
}
}
这就是我现在所拥有的
for(int 1 = 0; i<jsonObject.length(); i++){
JsonObject ob = jsonObject.getJsonObject(i);
JSONArray aboutVideo = ob.getJsonArray("thingsAboutTheVideo");
for(int j = 0 <aboutVideo.length(); j++){
JsonObject objectTwo = aboutVideo.getJsonObject(j);
}
}
哪种类型有效,但是当我试图获得第二个时它没有显示任何内容,如果我添加id == 303则应用程序崩溃如果显示什么都不知道我做错了什么
答案 0 :(得分:0)
for循环的入口点在哪里?什么&#39; jsonObject.length()
?我怀疑它是在第一个musicVideos数组索引内开始的。这意味着你只能获得那个对象的信息。无论如何,从头开始,希望它提供一些见解(使用org.json):
//content is a String containing the JSON Object you posted here (with an extra ]).
JSONArray musicVideosArray = new JSONObject(content).getJSONArray("musicVideos"); //We are only interested in the array
for (int i = 0; i < musicVideosArray.length(); i++) { //For each object in the array
JSONObject musicVideo = musicVideosArray.getJSONObject(i);
//You can already retrieve ID's here if that's all you're interested in:
//int musicVideoID = musicVideo.getInt("id");
//if (musicVideoID == 303) {//Do whatever}
JSONArray aboutTheVideoArray = musicVideo.getJSONArray("thingsAboutTheVideo"); //Get the 'things' array containing ID's
for (int j = 0; j < aboutTheVideoArray.length(); j++) {
JSONObject aboutTheVideo = aboutTheVideoArray.getJSONObject(j);
System.out.println("Object " + (j+1) + " of parent object " + (i+1) + ", ID: " + aboutTheVideo.getInt("id"));
//Do whatever
}
}
我还要注意,您在此处发布的JSON对象并不完全有效,您缺少]
字符来关闭musicVideos
数组。