2017-06-07 1 views
1

私はこの応答をサーバーから取得しています。私はpojoクラスを使ってそれを解析したい。私は値を保持するセッターメソッドとゲッターメソッドを持つpojoクラスを作成しました。しかし、私が使っているロジックは正しく動作していません。値を出力している間に値が正確に返されませんでした。アンドロイドのPOJOクラスを使用して各配列要素で異なるキーを持つJSONを解析する方法

応答:

{ 
     "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

あなたFeedPojoはすべて持っていないようですフィールド。 – benjosantony

+0

わかりやすさのためにいくつかのフィールドだけを投稿しました。しかし、私はすべてのソースコードを宣言しました。 @benjosantony – user1918566

答えて

0

私たちは、このよう FeedPojo.java

を解析することができます

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

Gsonで試してみてください。

Gradleのインポート:gsonで私たちのPOJOを解析する

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

はコード:

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

あなたFeedPojoはSerializableを実装する必要があります。

public class FeedPojo implements Serializable { 
    ... 
} 
関連する問題