The webservice return the following JSON string:
{"errorCode":0,"error":"","status":"OK","data":{"id":"1234A"}}
So to get a class that receives the response in a function like this that performs a post in Retrofit:
Call<UploadImageData> postData(@Header("Cookie") String sessionId, @Body UploadImageModal image);
I'd need to make a class like this;
public class UploadImageData {
private int errorCode;
private String error;
private String status;
}
But I'm lost in how I would have to declare the part that would take "data":{"id":"1234A"}, so it gets the data from there correctly.
How could I do this?
CodePudding user response:
Since data is a nested object within the surrounding json object, you can include it as another class in your UploadImageData class.
public class UploadImageData {
private int errorCode;
private String error;
private String status;
private MyDataClass data;
}
public class MyDataClass {
private String id;
}
Don´t forget setter methods oder make fields public.
CodePudding user response:
import net.sf.json.JSONObject;
String json ="{\"errorCode\":0,\"error\":\"\",\"status\":\"OK\",\"data\":{\"id\":\"1234A\",\"id\":\"14A\"}} ";
private static void resolveJson(String json) {
JSONObject jsonObj = JSONObject.fromObject(json);
System.out.println(jsonObj.get("errorCode"));
System.out.println(jsonObj.get("error"));
System.out.println(jsonObj.get("status"));
JSONObject data = jsonObj.getJSONObject("data");
System.out.println(data.get("id"));
}
Output:
0
OK
["1234A","14A"]