Unable to return JSON Object, within Object in Android app

前端 未结 3 453
有刺的猬
有刺的猬 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:33

    I'm not sure which data from your JSON response, you are trying to extract. But I think this example with Google Translate API will help you out.

    JSON Response:

    "data": {
        "translations": [
            {
                "translatedText": "Hallo Welt",
                "detectedSourceLanguage": "en"
            }
        ]
    }
    

    If I want to get the translatedText, "Hallo Welt", I do it like this:

    public String parseJSONForTranslation(String jsonString) {
        try {
            JSONObject object = (JSONObject) new JSONTokener(jsonString).nextValue();
            return object.getJSONObject("data").getJSONArray("translations").
                    getJSONObject(0).getString("translatedText");
        }
        catch (JSONException e) {
            return null;
        }
    }
    

    So as you can see, if you want to further extract information by going into the next "level" of your JSON response, you use either getJSONObject or getJSONArray (depending on if it's an array or not) until you reach the "level" where the data you want to extract is at. And you only use getString when you are at that last "level". Hopefully, this can help you out.

    0 讨论(0)
  • 2020-12-22 11:39

    You can create JSON array when create JSON object fail

    try {
        JSONObject jsonObject = new JSONObject(result);
        // do something
    }catch (JSONException e) {
        try{
        JSONObject jsonObject = new JSONArray(result);
        // do something
        }catch(JSONException e){
        //...
        }
    }
    
    0 讨论(0)
  • 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");
         }
      }
    }
    
    0 讨论(0)
提交回复
热议问题