使用改造转换 json 数组?

Coverting json array using retrofit?

这是我的Json

[  
{  
  "nata_center":{  
     "id":67,
     "nata_center_name":"Primo Institute of Design"
  }
 },
{  
  "nata_center":{  
     "id":68,
     "nata_center_name":"Sai Ganesh Institute"
  }
 }
]

波乔类

public class Explorer {
    NataCenter nataCenter;

    public NataCenter getNataCenter() {
        return nataCenter;
    }

    public void setNataCenter(NataCenter nataCenter) {
        this.nataCenter = nataCenter;
    }
}

2)

public class NataCenter {
    public String id;
    public String nata_center_name;

     public NataCenter(String id,String nata_center_name)
     {
      this.id=id;
     this.nata_center_name=nata_center_name;
     }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public String getNata_center_name() {
        return nata_center_name;
    }

    public void setNata_center_name(String nata_center_name) {
        this.nata_center_name = nata_center_name;
    }
}

GetMethhodinRetrofit

void getCenter(@Query("id") int id,Callback<List<Explorer>> callback);

MainActvitiy.java

       service.getCenter(i,new Callback<List<Explorer>>() {
                        @Override
                        public void success(List<Explorer> o, Response response) {
                            Log.d(TAG,"Success" + "Response"+o.toString());

                        @Override
                        public void failure(RetrofitError error) {
                            Log.d(TAG,"Failed"+error.toString());

                        }
                    });

这是错误信息

 Failedretrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

基本上问题是您在 json 中将数组作为根。您必须创建对象,该对象将用作此列表的容器和此对象的反序列化器。我使用资源管理器作为容器,如下所示:

public class Explorer {

    List<NataCenter> nataCenter;


    public List<NataCenter> getNataCenter() {
        return nataCenter;
    }

    public void add(NataCenter nataCenterItem){
        if(nataCenter == null){
            nataCenter = new LinkedList<>();
        }
        nataCenter.add(nataCenterItem);
    }
}

我的解决方案只是解释。您可以提高 Explorer class。 Setter 列表不是最好的主意。

NataCenter class 看起来像以前的,

其中一个重要的想法是ExplorerDeserializerJson class。它用于反序列化 json,如下所示:

public class ExplorerDeserializerJson implements JsonDeserializer<Explorer> {

    @Override
    public Explorer deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
            throws JsonParseException {
        Explorer explorer = new Explorer();
        JsonArray jsonArray = je.getAsJsonArray();
        Gson gson = new Gson();
        for(JsonElement element : jsonArray){
            JsonObject jsonObject = element.getAsJsonObject();
            NataCenter nataCenter = gson.fromJson(jsonObject.get("nata_center"), NataCenter.class);
            explorer.add(nataCenter);
        }
        return explorer;

    }
}

另外我换了你的客户。现在Explorer是回应。

void getCenter(@Query("id") int id,Callback<Explorer> callback);

最后,您必须在创建 RestAdapter 的地方注册新的解串器,如下所示:

 RestAdapter restAdapter = new RestAdapter.Builder()
                   .setEndpoint(BuildConfig.IP)
                    .setConverter(new GsonConverter(new GsonBuilder()
                            .registerTypeAdapter(Explorer.class, new ExplorerDeserializerJson())
                            .create()))
                    .build();
            restAdapter.create(CenterClient.class).getCenter(1);