使用动态数组和Object处理gson

时间:2016-07-07 04:48:49

标签: android json parsing gson

我从API获得响应,因为如果存在数据则返回对象,当数据不存在时,它返回空白数组。 我有用于序列化的模型类。 这是模型类

    @SerializedName("local")
    public Local _local  = new Local();     
     public class Local{
      @SerializedName("total")
            public String _total;

            @SerializedName("providers")
            public  ArrayList<ProvidersDetails> _providers = new ArrayList<>();

            public class ProvidersDetails{
                @SerializedName("id")
                public String _id;

                @SerializedName("image")
                public String _image;

            }

            @SerializedName("admin")
            public transient Admin admin = new Admin();

            public class Admin{

               @SerializedName("id")
                public String _id;

                @SerializedName("first_name")
                public String _first_name;

                @SerializedName("last_name")
                public String _last_name;

            }

           @SerializedName("orgn")
            public Organization _organization = new Organization();

            public class Organization{
                @SerializedName("name")
                public String _ name;
            }
        }

以下是我从api获得的回复的一部分

       "local":{
       "providers":[
        {
        "id":"1180",  
       "image":"photo.png"
        },
         {
        "id":"1180",  
       "image":"photo.png"
         },
         {
        "id":"1180",  
       "image":"photo.png"
        }
        ],
       "admin":{
       "id":"1180",
      "first_name":"nqgnerq",
       "last_name":"gnejbeqp",
      },
     "orgn":{
      "name":"organization name"
      }
      }

这是我在数据不存在时得到的另一种形式

     "local":{
     "total":0,
     "providers":[
     ],
     "admin":[
      ],
    "orgn":{
      "name":"organization name"
      }
     }

我已经检查了许多工作但是失败了,因为我想在我的pojo类中处理它。任何人都有解决方案,请建议。

1 个答案:

答案 0 :(得分:3)

您可以使用&#34; JsonDeserializer&#34;来处理它。这是一个例子。  上课ProvidersDetails implements Serializable

像这样创建一个新类

public class ProvidersDetailsList {
    public ArrayList<ProvidersDetails> getDetails() {
        return providersDetails;
    }

    ArrayList<ProvidersDetails> providersDetails= new ArrayList<>();
}

现在编写Deserializer。

 public class PhotoAlbumDeserializer implements JsonDeserializer {

        @Override
        public Object deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
            ProvidersDetailsList providersDetailsList= new ProvidersDetailsList ();

            JsonObject jsonObject = jsonElement.getAsJsonObject();
            if (jsonObject != null) {

                JsonElement usersField = jsonElement.getAsJsonObject().get("providers");
                if (usersField == null || usersField.isJsonNull() || usersField.isJsonPrimitive())
                    ; // if is null or is a primitive type will return an empty result
                else if (usersField.isJsonObject()) {
                    providersDetailsList.getDetails().add((ProvidersDetails) jsonDeserializationContext.deserialize(usersField, ProvidersDetails.class));

                } else if (usersField.isJsonArray()) {
                    Type listOfUserType = new TypeToken<List<ProvidersDetails>>() {
                    }.getType();
                    providersDetailsList.getDetails().addAll((Collection<? extends ProvidersDetails>) jsonDeserializationContext.deserialize(usersField, listOfUserType));
                }

            }
            return providersDetailsList;
        }
    }

现在称之为

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(ProvidersDetailsList.class, new PhotoAlbumDeserializer());
Gson gson = gsonBuilder.create();
ArrayList<ProvidersDetails> providersDetails = gson.fromJson(jsonString, ProvidersDetailsList.class);

以下是与Gson https://stackoverflow.com/a/16591621/3111083

相关的链接