Unable to return JSON Object, within Object in Android app

前端 未结 3 481
有刺的猬
有刺的猬 2020-12-22 11:12

So I have an Android app I am working on that makes an API call to the server and returns JSON information. Now in my android app I can parse the JSON but only up until the

3条回答
  •  情歌与酒
    2020-12-22 11:42

    Haven't actually tested this myself but I think something like this should work.

    The root element in your JSON is an array, so we will start there.

     public String parseJSONForTranslation(String json) throws JSONException {
      JSONArray array = new JSONArray(json);
    
      // We have an array, so we can loop through each inner object.
      for (int i = 0; i < array.length(); i++) {
    
         // Grab an object from the array so we can get the individual data
         JSONObject object = array.getJSONObject(i);
    
         // Variables that match the same key from your JSON
         int success = object.getInt("success");
         String error = object.getString("error");
    
         JSONObject response = object.getJSONObject("response");
         int lastSeen = response.getInt("lastseen");
         String tagColor = response.getString("tag_color");
         String name = response.getString("name");
         int rank = response.getInt("rank");
         String avatar = response.getString("avatar");
         String tag = response.getString("tag");
         int playTime = response.getInt("playtime");
         int percent = response.getInt("percent");
    
         JSONObject mapstats = response.getJSONObject("mapstats");
         int highestTier = mapstats.getInt("highest_tier");
         int numBeaten = mapstats.getInt("num_beaten");
         int percentCompletion = mapstats.getInt("percent_completion");
    
         JSONArray maps = mapstats.getJSONArray("maps");
         for (int i = 0; i < maps.length(); i++) {
            JSONObject map = maps.getJSONObject(i);
            int tier = map.getInt("tier");
            int modeId = map.getInt("modeid");
            String modeName = map.getString("mode_name");
            String modeColor = map.getString("mode_color");
            int bonus map.getInt("bonus");
            String name = map.getString("name");
            int rank = map.getInt("rank");
            String time = map.getString("time");
            int numCompleted = map.getInt("num_completed");
         }
      }
    }
    

提交回复
热议问题