2017-06-07 1 views
1

J'ai cette réponse venant du serveur. Je veux analyser cela en utilisant des classes pojo. J'ai créé des classes pojo ayant des méthodes setter et getter qui contiennent des valeurs. Mais la logique que j'utilise ne fonctionne pas correctement. Pendant que j'imprimais des valeurs, elle ne renvoyait pas les valeurs exactement.Comment analyser JSON ayant différentes clés dans chaque élément de tableau en utilisant des classes POJO dans android

Réponse:

{ 
     "code":200, 
     "status":"ok", 
     "message":"Promotions feed.", 
     "data":{ 
     "feed":[ 
     { 
     "id":0, 
     "businessId":0, 
     "photoUrl":"https:\/\/d1e6yi6s3cx2ur.cloudfront.net\/videos\/0\/8b84c9c1-50ed-4e93-9785-797bbf2be667.png", 
     "videoUrl":"https:\/\/d1e6yi6s3cx2ur.cloudfront.net\/videos\/0\/_464b2fb6-ede6-403f-b6ae-1666c9504337.mov", 
     "isNew":true, 
     "type":"news" 
     }, 
     { 
     "id":198, 
     "specialType":"limited", 
     "name":"Big Data Shoes!", 
     "description":"Get them now while you can. They're dangerous!", 
     "start":"2017-06-04 14:59:01Z", 
     "end":"2018-01-26 19:00:00Z", 
     "limitedAmount":30, 
     "countInfo":{ 
     "views":70, 
     "likes":0, 
     "liked":false, 
     "type":"count_info" 
     }, 
     "type":"special" 
     }, 
     { 
     "id":"1616636", 
     "partnerId":3, 
     "uuid":"57dacc36-abac-4bb3-89a4-f1981130b206", 
     "type":"partner_offer" 
     } 
    ] 
    } 
} 

FeedPojo.java

public class FeedPojo { 

    private static String id; 
    private static String businessId; 
    private static String photoUrl; 
    private static String videoUrl; 
    public static String getId() { 
     return id; 
    } 
    public static void setId(String id) { 
     FeedPojo.id = id; 
    } 

    public static String getPartnerId() { 
     return partnerId; 
    } 

    public static void setPartnerId(String partnerId) { 
     FeedPojo.partnerId = partnerId; 
    } 
    } 

MainActivity.java

FeedPojo.setType(promoJsonObject.getString("type")); 

Log.d(TAG, "TypeFeed:" + FeedPojo.getType());  
Log.e(TAG, "Type:" + promoJsonObject.getString("type"));  
      if (promoJsonObject.getString("type") == "news") { 

             FeedPojo.setId(promoJsonObject.getString("id")); 
             FeedPojo.setBusinessName(promoJsonObject.getString("businessName")); 
             FeedPojo.setBusinessLogoUrl(promoJsonObject.getString("businessLogoUrl")); 
             FeedPojo.setText(promoJsonObject.getString("text")); 

Log.d(TAG,"Id"+FeedPojo.getId()); 
             Log.d(TAG,"Businessname"+FeedPojo.getBusinessName()); 

            } 
            if (FeedPojo.getType() == "special") { 

             FeedPojo.setId(promoJsonObject.getString("id")); 
             FeedPojo.setType(promoJsonObject.getString("type")); 
             FeedPojo.setName(promoJsonObject.getString("name")); 
             FeedPojo.setDescription(promoJsonObject.getString("description")); 
             FeedPojo.setStart(promoJsonObject.getString("start")); 
             FeedPojo.setEnd(promoJsonObject.getString("end")); 
       } 
+0

Votre FeedPojo ne semble pas avoir toutes les des champs. – benjosantony

+0

Je viens de publier seulement quelques champs juste pour rendre facile à comprendre .. Mais j'ai déclaré tous dans mon code source. @benjosantony – user1918566

Répondre

0

Nous pouvons analyser comme ça, FeedPojo.java

public class FeedPojo { 
    int id; 
    int businessId; 
    int limitedAmount; 
    int views; 
    int likes; 
    int partnerId; 
    String photoUrl; 
    String videoUrl; 
    String type; 
    String specialType; 
    String name; 
    String description; 
    String start; 
    String end; 
    String countInfoType; 
    String uuid; 
    boolean isNew; 
    boolean liked; 
} 

FeedParser.java

public class FeedParser { 
    public static void parse(String response) { 
     try { 
      JSONObject feedObject = new JSONObject(response); 

      if (feedObject == null) { 
       return; 
      } 

      JSONArray feedArray = feedObject.optJSONArray("feed"); 

      if (feedArray == null) { 
       return; 
      } 

      ArrayList<FeedPojo> feedPojos = new ArrayList<>(); 
      for (int i = 0; i < feedArray.length(); i++) { 
       JSONObject feed = feedArray.optJSONObject(i); 

       if (feed == null) { 
        continue; 
       } 

       FeedPojo feedPojo = new FeedPojo(); 
       feedPojo.id = feed.optInt("id", 0); 
       feedPojo.businessId = feed.optInt("businessId", 0); 
       feedPojo.limitedAmount = feed.optInt("limitedAmount", 0); 
       feedPojo.partnerId = feed.optInt("partnerId", 0); 

       feedPojo.photoUrl = feed.optString("photoUrl", ""); 
       feedPojo.type = feed.optString("type", ""); 
       feedPojo.specialType = feed.optString("specialType", ""); 
       feedPojo.name = feed.optString("name", ""); 
       feedPojo.description = feed.optString("description", ""); 
       feedPojo.start = feed.optString("start", ""); 
       feedPojo.end = feed.optString("end", ""); 
       feedPojo.uuid = feed.optString("uuid", ""); 

       feedPojo.isNew = feed.optBoolean("isNew", false); 
       feedPojo.isNew = feed.optBoolean("isNew", false); 

       JSONObject countInfoObj = feed.optJSONObject("countInfo"); 

       if (countInfoObj == null) { 

        feedPojos.add(feedPojo); 
        continue; 
       } 

       feedPojo.views = countInfoObj.optInt("views", 0); 
       feedPojo.likes = countInfoObj.optInt("likes", 0); 
       feedPojo.liked = countInfoObj.optBoolean("liked", false); 
       feedPojo.countInfoType = countInfoObj.optString("type", ""); 

       feedPojos.add(feedPojo); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
0

Essayez avec Gson.

importation Gradle:

compile 'com.google.code.gson:gson:2.2.4' 

Code pour analyser notre POJO avec gson:

JSONObject object = new JSONObject(response); 
Gson gson = new Gson(); 
FeedPojo feedPojo = gson.fromJson(object.toString(), FeedPojo.class); 

Votre FeedPojo doit mettre en œuvre Serializable:

public class FeedPojo implements Serializable { 
    ... 
}