Coverting json array using retrofit?

寵の児 提交于 2019-12-19 02:47:10

问题


this is my Json

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

Pojo classes

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());

                        }
                    });

This is the error message

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

回答1:


Basically the problem is that you have array as root in json. You have to create object which will be used as container for this list and deserializator for this object. I used Explorer as container, it looks like below:

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);
    }
}

My solution is just explanation. You can improve your Explorer class. Setter for list is not the best idea.

The NataCenter class looks like previous,

The one of important think is ExplorerDeserializerJson class. It is used to deserialize json and it looks like below:

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;

    }
}

Additionally I change your client. Now Explorer is a response.

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

As the last you have to register new deserializer in the place where you create RestAdapter as is shown below:

 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);


来源:https://stackoverflow.com/questions/29169640/coverting-json-array-using-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!