我有这个json url输出这个(片段)
{
"status":true,
"result":{
"message":"Successfully retrieved the daily feed.",
"items":{
"1376438400":[
{
"code":"DjCr3N3o",
"slug":"soulja-boy-gets-kicked-off-airplane",
"cdn_screenshot_path":"screenshots\/2013\/08\/DjCr3N3o.png",
"title":"Soulja Boy Gets Kicked Off Airplane!",
"hits":"457",
"date_added":"1376507797"
},
{
"code":"7V9eOVpX",
"slug":"dr.-dre-and-suge-knight-baby-mama-michelle-surprise-performance-she-sounds-like-a-chipmunk-but-sings-like-an-angel",
"cdn_screenshot_path":"screenshots\/2013\/08\/7V9eOVpX.png",
"title":"Dr. Dre AND Suge Knight Baby Mama Michel'le Surprise Performance! (She Sounds Like A Chipmunk But Sings Like An Angel!)",
"hits":"525",
"date_added":"1376505010"
},
{
"code":"8ovO203r",
"slug":"headless-snake-bites-itself-in-the-butt",
"cdn_screenshot_path":"screenshots\/2013\/08\/8ovO203r.png",
"title":"Headless Snake Bites Itself In The Butt!",
"hits":"361",
"date_added":"1376485812"
}
],
"1376352000":[
{
"code":"9b9jR6qs",
"slug":"show-you-how-to-do-this-son-chris-paul-hits-4-straight-jumpers-on-colleges-best-point-guards",
"cdn_screenshot_path":"screenshots\/2013\/08\/9b9jR6qs.jpg",
"title":"Show You How To Do This Son! Chris Paul Hits 4 Straight Jumpers On College's BEST Point Guards!",
"hits":"979",
"date_added":"1376443810"
},
{
"code":"p6l5pwg8",
"slug":"ttbnez-fck-da-opp-music-video",
"cdn_screenshot_path":"screenshots\/2013\/08\/p6l5pwg8.png",
"title":"TTBNEZ - F*ck Da Opp [Music Video]",
"hits":"316",
"date_added":"1376419812"
},
{
"code":"haxUoUVt",
"slug":"strip-life-the-reality-series-feat.-lanipop-entyce-trailer",
"cdn_screenshot_path":"screenshots\/2013\/08\/haxUoUVt.png",
"title":"Strip Life: The Reality Series (feat. Lanipop, Entyce) [Trailer]",
"hits":"426",
"date_added":"1376419214"
}
],
我遇到的问题是弄清楚如何解析它的格式以及如何获取诸如“code”,“slug”和“title”之类的数据。这是我到目前为止所做的,但它似乎是错误的,因为我可能必须有2个循环而不是1个我认为。
这是我到目前为止所拥有的
@Override
protected Void doInBackground(Void... params) {
// Create the array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrive JSON Objects from the given website URL in JSONfunctions.class
jsonobject = JSONfunctions
.getJSONfromURL("http://api.hoodplug.com/v1/videos/daily_feed?per_page=5&offset=0&format=json");
try {
// Locate the array name
jsonarray = jsonobject.getJSONArray("item");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("code", jsonobject.getString("code"));
map.put("slug", jsonobject.getString("slug"));
map.put("title", jsonobject.getString("title"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
答案 0 :(得分:1)
你在这里错过了一点点!
在这里你得到了第一个json对象:
jsonobject = JSONfunctions
.getJSONfromURL("http://api.hoodplug.com/v1/videos/daily_feed?per_page=5&offset=0&format=json");
这个json对象是包含状态和结果的整个大JSON对象,但是你直接想要访问结果对象里面的JSON数组!用这个:
// Locate the array name
jsonarray = jsonobject.getJSONArray("items");
正确的是你必须首先从带有
的大json对象获取结果对象JSONObject resultObject = jsonobject.getJSONObject("result");
然后使用resultObject来获取数组!
try {
// Locate the array name
jsonarray = resultObject .getJSONArray("item");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("code", jsonobject.getString("code"));
map.put("slug", jsonobject.getString("slug"));
map.put("title", jsonobject.getString("title"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
我希望你理解我的答案,但如果你对我的答案有其他疑问,请随意提问! :)