簡體   English   中英

Exclude null values from dynamic nested json using GSON Retrofit in Android

[英]Exclude null values from dynamic nested json using GSON Retrofit in Android

我正在解析動態嵌套 json 使用 gson 和 Retrofit 在 ZE84E30B9390CDB64DDB6DB.C 我想在將它添加到列表之前排除空值/空值。

我嘗試了以下方法,但它們似乎都不起作用:

 // retMap.values().removeIf(Objects::isNull);
 //  Collection<POJOContent> values = retMap.values();
//  while (values.remove(null)) {}

//list.removeAll(Arrays.asList(""));

自定義解串器.java

public class CustomDeserializer implements JsonDeserializer<MyContentWrapper> {
    private final String abc_key = "abc";

    @Override
    public MyContentWrapper deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {


        MyContentWrapper abc = new MyContentWrapper();
        JsonObject object = json.getAsJsonObject().getAsJsonObject(abc_key);
        List<POJOContent> list = new ArrayList<POJOContent>();
        System.out.println("New Data: "+object);

        for (Map.Entry<String, JsonElement> set : object.entrySet()) {
            System.out.println("LIST " + set);

            JsonObject nextObject = object.getAsJsonObject().getAsJsonObject(set.getKey());


           Map<String, POJOContent> retMap = new Gson().fromJson(nextObject, new TypeToken<LinkedHashMap<String, POJOContent>>() {
            }.getType());

            // retMap.values().removeIf(Objects::isNull);
              //  Collection<POJOContent> values = retMap.values();
              //  while (values.remove(null)) {}

                         list.addAll(retMap.values());
                //list.removeAll(Arrays.asList(""));
        
          

        }
        abc.abc = list;
        return abc;
    }

POJOContent.java

        
public class POJOContent {

    @SerializedName("ab")
    public String content;

    @SerializedName("id")
    public String id;

    @SerializedName("key")
    public String key;

    @Override
    public String toString() {
        return content;
    }

    //getters and setters

}

MyContentWrapper.java

public class MyContentWrapper {

    public List<POJOContent> abc;
}

JSON:

{
    "abc": {
        "1": {
            "1": {
                "ab": "some content",
                "id": "240",
                "key": "value"
            },
            "2": {
                "ab": "some content",
                "id": "240",
                "key": "value"
            },
            "3": {
                "ab": "some content",
                "id": "240",
                "key": "value"
            }
        },
        "2": {
            "1": {
                "ab": "some content",
                "id": "241",
                "key": "value"
            },
            "2": {
                "ab": "some content",
                "id": "241",
                "key": "value"
            },
            "3": {
                "ab": "some content",
                "id": "241",
                "key": "value"
            }
        },
        "3": {
            "1": {
                "ab": "",
                "id": "252",
                "key": "value"
            },
            "2": {
                "ab": "some content",
                "id": "252",
                "key": "value"
            },
            "3": {
                "ab": "",
                "id": "252",
                "key": "value"
            }
        }
    }
}

基本上,我想跳過“ab”值為空的對象,而不是將它們添加到列表中(如果“ab”值為空,則其他鍵無關)。 澄清一下,如果父 object 有任何子 object 的“ab”鍵為空值,則應排除子 object。 如果父 object 的所有子級的“ab”鍵為空值,則應排除父級 object。

我錯過了什么嗎? 將不勝感激一些幫助。

編輯::預期 JSON:

{
    "abc": {
        "1": {
            "1": {
                "ab": "some content",
                "id": "240",
                "key": "value"
            },
            "2": {
                "ab": "some content",
                "id": "240",
                "key": "value"
            },
            "3": {
                "ab": "some content",
                "id": "240",
                "key": "value"
            }
        },
        "2": {
            "1": {
                "ab": "some content",
                "id": "241",
                "key": "value"
            },
            "2": {
                "ab": "some content",
                "id": "241",
                "key": "value"
            },
            "3": {
                "ab": "some content",
                "id": "241",
                "key": "value"
            }
        },
        "3": {
            "2": {
                "ab": "some content",
                "id": "252",
                "key": "value"
            }
        }
    }
}

更新 2:

這是我將 List 更改為 Maps 后得到的響應:

        Callback<MyContentWrapper> myCallback = new Callback<MyContentWrapper>() {
        @Override
        public void onResponse(Call<MyContentWrapper> call, Response<MyContentWrapper> response) {
            if (response.isSuccessful()) {
                Log.d("Callback", " Message: " + response.raw());
                Log.d("Callback", " Message: " + response.body().abc.values());
                still showing empty brackets here --->  
//Log.d =     Message: [{1=1. some content, 1=2. some content, 1=3. some content}, {}]
            } else {
                Log.d("Callback", "Code: " + response.code() + " Message: " + response.message());
            }
        }
        @Override
        public void onFailure(Call<MyContentWrapper> call, Throwable t) {
            t.printStackTrace();
        }
    };

僅作為上下文,這是對上述回調的 JSON 響應:


{
    "abc": {
        "1": {
            "1": {
                "ab": "some content",
                "id": "240",
                "key": "value"
            },
            "2": {
        "ab": "some content",
        "id": "240",
        "key": "value"
            },
      "3": {
        "ab": "some content",
        "id": "240",
        "key": "value"
            }
        },
        "2": {
            "1": {
          "ab": "",
          "id": "241",
          "key": "value"
            },
            "2": {
          "ab": "",
          "id": "241",
          "key": "value"
            },
      "3": {
          "ab": "",
          "id": "241",
          "key": "value"
      }
        }
    }
}

首先,您的MyContentWrapper是錯誤的。 根據您的 JSON,它不是一個List 它應該是地圖的 map,如下所示:

public class MyContentWrapper {
    public Map<Integer, Map<Integer, POJOContent>> abc;
}

現在,您可以為這種結構編寫一些復雜的反序列化器,但還有另一種方法可以做到這一點。 即,如果您聲明助手Map如下:

@SuppressWarnings("serial")
public class MyMap extends HashMap<Integer, POJOContent> {
    @Override
    public POJOContent put(Integer key, POJOContent value) {
        if(null==value.getContent() || value.getContent().isBlank()) {
            return null;
        }
        // Added only if content = "ab" is not blank.
        return super.put(key, value);
    }
}

然后調整MyContentWrapper

public class MyContentWrapper {
    public Map<Integer, MyMap> abc;
}

那么它只是:

MyContentWrapper mcw = new Gson().fromJson(JSON, MyContentWrapper.class);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM