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();
3
  • Check : stackoverflow.com/questions/26814673/… Commented Dec 5, 2014 at 6:31
  • My issue is that the custom deserializer is not being called. Commented Dec 5, 2014 at 6: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 Commented Dec 5, 2014 at 7:07

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.