0

我有这个输出这个(片段)的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"
            }
         ],

我遇到的问题是弄清楚如何解析它,因为它的格式以及如何访问诸如“代码”、“slug”和“标题”之类的数据。这是我到目前为止所拥有的,但这似乎是错误的,因为我可能必须有 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;
    }
4

1 回答 1

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();
        }

我希望您能理解我的回答,但如果您对我的回答有其他问题,请随时提问!:)

于 2013-08-15T04:46:13.173 回答