How can I parse this JSON in Android?

后端 未结 1 654
独厮守ぢ
独厮守ぢ 2020-11-27 22:26

I want to pull out the user block. The JSON result will always change, sometimes 4 users will be returned, sometimes 10 etc.

 {
      \"results\": [
                 


        
相关标签:
1条回答
  • 2020-11-27 23:06

    Use the JSONObject

     // Get some JSON from wherever
     String json = getJSONFromServer();
    
     // Parse the JSON response into an object
     JSONObject object = new JSONObject(json);
    
     // Get the results array
     JSONArray users = object.getJSONArray("results");
     for(int i = 0; i < users.length(); i++) {
         // Each element in the results array is a JSONObject with a single
         // property "user" which is a JSONObject that contains the user data
         JSONObject user = users.getJSONObject(i).getJSONObject("user");
    
         // Do something with the user
         String firstName = user.getString("first_name");
     }
    
    0 讨论(0)
提交回复
热议问题