google-httpclient JSON definition - java

When the JSON is:
{
"result":200,
"data", "my data"
}
The definition of the class can be:
class HttpResult
{
#Key
public int result;
#Key
public String data;
}
When the JSON root is a array and has no Key:
[
{
"result:200",
"data":"data1"
},
{
"result":404,
"data":"data2"
}
]
Then the definition of the class is?

Same class definition, it's just a Collection of them instead of one.

Related

Id json deserialization with Jackson MixIn (for null values)

I've a problem with deserialization of Id class. I have class:
public class Id{
private String raw;
public Id(String raw) {
this.raw = raw;
}
public abstract String getRaw() {
return raw;
}
}
and MixIn:
abstract class IdMixIn {
#JsonCreator
public IdMixIn(String raw) {
}
#JsonValue
public abstract String getRaw();
}
and json array:
[
"test-id",
"null",
null
]
First two Id-s after deserialization are correct - they are (Id(raw=null)) but the third one is simply null. Can I somehow achieve same effect for "null" and null?
Thanks for help!

How to convert similar looking JSON structure into different POJO with Gson?

I am having below 2 JSON, one for login and another one for order
{
"head": {
"requestCode": "code"
},
"body": {
"reqId": "xyz",
"userName": "xyz",
"passwd": "xyz",
}
}
{
"head": {
"requestCode": "code"
},
"body": {
"reqId": "xyz",
"orderId": "xyz"
}
}
I am trying to write java pojo where the head and refId of body are common for each json but other content of body.
something like the below pojo. Now problem is GSon cannot parse and build objects based on nested parameterized types. Is there any better way to implement it? Not JSON structure will not change.
POJO
public class Base<T extends Body> {
#SerializedName("head")
#Expose
public Head head;
#SerializedName("body")
#Expose
private T body;
}
public class Body {
#SerializedName("clientCode")
#Expose
private String clientCode;
}
public class Head {
#SerializedName("requestCode")
#Expose
public String requestCode;
}
public class Login extends Body {
#SerializedName("userName")
#Expose
public String userName;
#SerializedName("passwd")
#Expose
public String passed;
}
public class Order extends Body {
#SerializedName("orderId")
#Expose
String orderId;
}
You could either introduce two new subclasses of Base which you use for deserialization, for example:
class LoginRequest extends Base<Login> { }
class OrderRequest extends Base<Order> { }
Or you can use Gson's TypeToken class to deserialize a parameterized Base type. See also the corresponding section in the user guide. For example:
Type loginType = new TypeToken<Base<Login>>() {}.getType();
Base<Login> loginRequest = gson.fromJson(..., loginType);

Unable to map API response to pojo

I have a json response
"data": {
"students": [
{
"id": 100,
"name": "ABC"
},
{
"id": 101,
"name": "XYZ"
}
I need to map it to my pojo, something like -
public class TempClass {
List<Temp> list_students;
}
class Temp {
Long id;
String name;
}
Direct reading API response into my pojo gives me a class cast exception. I've tried converting response to a list of map and the collect as Temp class but that also doesn't work.
Exception -
java.util.LinkedHashMap cannot be cast to java object
Any suggestions please?
Code snippet for conversion -
new TempClass(((LinkedHashMap<String, Object>) response.getData()).entrySet())
.stream().map(map -> mapper.convertValue(map, Temp.class))
.collect(Collectors.toList()))
public class Data{
public ArrayList<Student> students;
}
public class Root{
public Data data;
}
public class Student{
public int id;
public String name;
}
Your POJO class will look like this

Problem with model in Java for given JSON

I need to create a model in Java to deseralize such JSON.
[
{
"yyy": {
"address": "y-a",
"acronym": "YYY"
},
"xxx": {
"address": "x-a",
"acronym": "XXX"
}
},
{
"vvv": {
"address": "v-a",
"acronym": "VVV"
}
}
]
It looks for me that a model in Java will look like here (i'm using Jackson)
public class Yyy{
public String address;
public String acronym;
}
public class Xxx{
public String address;
public String acronym;
}
public class Vvv{
public String address;
public String acronym;
}
public class Root{
public Yyy yyy;
public Xxx xxx;
public Vvv vvv;
}
It is not acceptable at all because I don't know which keys like xxx, yyy, vvv I will get -
They are not defined and can be random. What I need to do to convert this JSON to have something usefull? I mean to have an array of objects and in inside object a map where keys will be created from keys in JSON (vvv, yyy, xxx)

Retrofit convert array of objects to GSON Error

I am using Retrofit to make a HTTP request which returns an array of object and I am getting the following errors:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
The response returned is expected to be like this:
[ {key1: "value1", key2: "value2"}, {key1: "value1", key2: "value2"}, ... ]
I have the following class, for serializing the data:
public class data {
private List<element> dataList;
public List<element> getElements() {
return dataList;
}
public class element {
#SerializedName("key1")
private String key1;
#SerializedName("key2")
private String key2;
// Getters and Setters
}
}
Please let me know if you have any ideas. Thanks
The error was actually in my implementation of Retrofit Callback. My implementation was expecting an object when it should be expecting an array in this case. Thanks everyone for the help.
Before
//*****MyData*****//
public class MyData {
private List<Data> dataList;
public List<Data> getElements() {
return dataList;
}
public class Data {
#SerializedName("key1")
private String key1;
#SerializedName("key2")
private String key2;
// Getters and Setters
}
}
//*****Callback Implementation*****//
public class MyDataCallback extends Callback {
public MyDataCallback(MyDataCallbackListener<MyData> myDataCallbackListener) {
super(myDataCallbackListener);
}
#Override
public void success(MyData data, Response response) {
if (myDataCallbackListener != null) {
myDataCallbackListener.onCallbackComplete(true, response, MyDataCallback.CALLBACK_SUCCESS_MESSAGE, data);
}
}
}
After
//*****Data*****//
public class Data {
#SerializedName("key1")
private String key1;
#SerializedName("key2")
private String key2;
// Getters and Setters
}
//*****Callback Implementation*****//
public class MyDataCallback extends Callback {
public MyDataCallback(MyDataCallbackListener<List<Data>> myDataCallbackListener) {
super(myDataCallbackListener);
}
#Override
public void success(List<Data> data, Response response) {
if (myDataCallbackListener != null) {
myDataCallbackListener.onCallbackComplete(true, response, MyDataCallback.CALLBACK_SUCCESS_MESSAGE, data);
}
}
}
As Dave mentioned in his comment, it does seem strange that you have recursion in the class that I am assuming is your response object. (your class "data" has a list of "data" objects).
I would suggest something a little more strait forward such as this:
public class ResponseObject {
private ArrayList<DataObject> mDataObjects;
public ArrayList<DataObject> getDataObjects() {
return mDataObjects;
}
private class DataObject {
private String key1;
private String key2;
public String getKey1() {
return key1;
}
public String getKey2() {
return key2;
}
}
}
or since you are local maybe you can buy Jake a beer :) From his photo, I would check Rouge Ales, 21 Amendment or my favorite last time I was in SF - Magnolia
It's not valid JSON to begin with an array. You need to instead return something like this:
{
dataList: [
{
key1: "value1",
key2: "value2"
},
{
key1: "value3",
key2: "value4"
}
]
}
Then you can use GSON to deserialize that into your data class.

Categories

Resources