0

I am trying to implement deserialization to parse json object as a string, but my custom deserializable class is not being called.

JSON which needs to be parsed

{

 "status": true,
 "student": [
    {
        "id": 1,
        "name": "",
        "age": "",
         "title": "",

    }
]
}

My Deserializable class

public class MyDeserializer implements JsonDeserializer<StudentData> {

@Override
public StudentData deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) {
    try {

        String content = je.getAsJsonObject().get("student").getAsString();

        return new Gson().fromJson(content, StudentData)
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
}

Register my deserializer:-

MyDeserializer myDeserializer = new MyDeserializer();
Gson gson = new GsonBuilder().registerTypeAdapter(NotificationResponse.class, myDeserializer).create();
mRestAdapter = new RestAdapter.Builder().setServer(baseUrl).setConverter(new GsonConverter(gson)).setLogLevel(RestAdapter.LogLevel.FULL).setRequestInterceptor(new RequestInterceptor() 
             {
                @Override
                public void intercept(RequestFacade requestFacade) {
                }
            }).build();
Kaushik
  • 6,150
  • 5
  • 39
  • 54
swati
  • 1,263
  • 2
  • 17
  • 27
  • Check : http://stackoverflow.com/questions/26814673/android-jsonarray-to-arraylist/26814776#26814776 – Haresh Chhelana Dec 05 '14 at 06:31
  • My issue is that the custom deserializer is not being called. – swati Dec 05 '14 at 06:34
  • to correctly implement you should use the same "gson" object you created to call "fromGson()" function ? otherwise initialising it every time like new Gson() will not call your Desiralizer class – Shubhang Malviya Dec 05 '14 at 07:07

2 Answers2

0

I think this tutorial will help you implement a Deserializer(and might introduce some new concepts)

Try it, and see if it work for you!

Shubhang Malviya
  • 1,525
  • 11
  • 17
0

For something that simple I don't think adding Gson as a dependency is worth it.

Example:

JSONObject jObj = new JSONObject(theJsonYouPostedAbove);
boolean status = jObj.getBoolean("status");
JSONArray jArr = jObj.getJSONArray("student");
for (int i = 0; i < jArr.length(); i++) {
    JSONObject jo = jArr.getJSONObject(i);
    int id = jo.getInt("id");
    String name = jo.getString("name");
    ...
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148