Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用Gson跳过空条目_Android_Json_Gson - Fatal编程技术网

Android 如何使用Gson跳过空条目

Android 如何使用Gson跳过空条目,android,json,gson,Android,Json,Gson,使用Gson反序列化JSON时,是否有方法跳过JSON数组中的空条目 [ { text: "adsfsd...", title: "asdfsd..." }, null, { text: "adsfsd...", title: "asdfsd..." } ] 结果列表有3个条目,第二个条目为空。我想将Gson配置为跳过空值,但我找不到一种方法。默认情况下,只要不将serializeNulls

使用Gson反序列化JSON时,是否有方法跳过JSON数组中的空条目

[
    {
        text: "adsfsd...",
        title: "asdfsd..."
    },
    null,
    {
        text: "adsfsd...",
        title: "asdfsd..."
    }
]

结果列表有3个条目,第二个条目为空。我想将Gson配置为跳过空值,但我找不到一种方法。默认情况下,只要不将
serializeNulls()
设置到GsonBuilder,空值将被排除。一旦我找到这个。为我工作:

class CollectionAdapter implements JsonSerializer<Collection<?>> {
  @Override
  public JsonElement serialize(Collection<?> src, Type typeOfSrc, JsonSerializationContext context) {
    if (src == null || src.isEmpty()) // exclusion is made here
      return null;

    JsonArray array = new JsonArray();

    for (Object child : src) {
      JsonElement element = context.serialize(child);
      array.add(element);
    }

    return array;
  }
}

希望这有帮助:)

默认情况下,只要不将GsonBuilder设置为
serializeNulls()
,就会排除空值。一旦我找到这个。为我工作:

class CollectionAdapter implements JsonSerializer<Collection<?>> {
  @Override
  public JsonElement serialize(Collection<?> src, Type typeOfSrc, JsonSerializationContext context) {
    if (src == null || src.isEmpty()) // exclusion is made here
      return null;

    JsonArray array = new JsonArray();

    for (Object child : src) {
      JsonElement element = context.serialize(child);
      array.add(element);
    }

    return array;
  }
}

希望这有帮助:)

您可以通过编写自己的自定义gson JsonDeserializer来排除空值

假设你有你的模型课

 class GetData {
    private String title;
    private String text;
}

class CustomDeserializer implements JsonDeserializer<List<GetData>> {

    @Override
    public List<GetData> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {

       JsonArray jsonArray =    jsonElement.getAsJsonArray();

       List<GetData>  list=new ArrayList<>(30);
       Gson gson = new Gson();

       for (JsonElement element : jsonArray) {
           // skipping the null here, if not null then parse json element and add in collection
           if(!(element instanceof JsonNull))
           {
               list.add(gson.fromJson(element,  GetData.class));
           }
        }

        return list;
    }

您可以通过编写自己的自定义gson JsonDeserializer来排除空值

假设你有你的模型课

 class GetData {
    private String title;
    private String text;
}

class CustomDeserializer implements JsonDeserializer<List<GetData>> {

    @Override
    public List<GetData> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {

       JsonArray jsonArray =    jsonElement.getAsJsonArray();

       List<GetData>  list=new ArrayList<>(30);
       Gson gson = new Gson();

       for (JsonElement element : jsonArray) {
           // skipping the null here, if not null then parse json element and add in collection
           if(!(element instanceof JsonNull))
           {
               list.add(gson.fromJson(element,  GetData.class));
           }
        }

        return list;
    }

它应该自动处理。它应该自动处理的错误是什么。什么是错误谢谢,但我正在寻找反序列化的解决方案。serializeNulls()仅影响序列化。您的自定义序列化程序在这里也没有帮助。谢谢,但我正在寻找反序列化的解决方案。serializeNulls()仅影响序列化。此外,您的自定义序列化程序在这里也没有帮助。谢谢,自定义反序列化程序可能是我需要的,但我希望有更简单的方法,例如序列化的serializeNulls(),所以在接受之前我会再等待一段时间。是的,如果有更简单的方法排除null元素,肯定会更好。不幸的是,没有其他方法:(谢谢,我可能需要一个自定义反序列化程序,但我希望有更简单的方法,比如序列化的serializeNulls(),所以我会再等一段时间再接受。是的,如果有更简单的方法排除null元素,肯定会更好。不幸的是,没有其他方法:(