2

Retrofit JSON Response:

{ "data": [{ "id": 1224410, "amount": "900200.00", "meta": { "referenceId": "referenceId","time": "2017-11-01T15:42:26Z","type": "send" } }], "version": "v2" } 

These are model structures:

public class MyOject { @SerializedName("id") private int mId; @SerializedName("amount") private double mAmount; @SerializedName("meta") private JSONObject mMeta; } 

I always get null in the mMeta variable

Update code class Wrapper using to parse response from JSONObject:

public class ResponseWrapper<DataType> { @SerializedName("data") private DataType mData; @SerializedName("version") private String mVersion; } 

This is Callback. I use it to specify the type of data to receive.

public class CallbackWithData<T> implements Callback<ResponseWrapper<T>> { @Override public final void success(ResponseWrapper<T> data, Response response) { this.success(data.getData(), data.getVersion()); } public abstract void success(T data, String version); } 

This is the API I use, which defines the desired object.

 @GET("/transaction") void listTransactions(@Header(Constants.Header.LOGIN_SECRET) String loginSecret, CallbackWithData<List<MyObject>> callback); 
15
  • put the code when you are parsing the data Commented Nov 24, 2017 at 3:29
  • @diegoveloper I using Retrofit and default GsonConverter to parsing response. Commented Nov 24, 2017 at 3:32
  • why are you using double on mAmount if your json is a String value Commented Nov 24, 2017 at 3:32
  • Using RestAdapter.Builder restBuilder = new RestAdapter.Builder() .setClient(new OkClient(new OkHttpClient())) .setConverter(new GsonConverter(gson)) and auto parsing to MyObject The values I have declared through the annotation Commented Nov 24, 2017 at 3:34
  • Could you put the retrofit interface ? Commented Nov 24, 2017 at 3:35

1 Answer 1

4

Use HashMap instead of JSONObject :

 @SerializedName("meta") private HashMap mMeta; 
Sign up to request clarification or add additional context in comments.

3 Comments

I'm glad to help you :)
@diegoveloper Can you explain little more.How it was work completely
so looks like gson can't deserialize json object into JSONObject class, then the HashMap is a valid option

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.