Problems Organizing Complex Json Response With Realm
I am trying to use realm with my android application. I tried looking in to some simple examples about realm. I was able to setup the project with realm and now want to start imple
Solution 1:
There is an excellent tool for this job - go to http://www.jsonschema2pojo.org/ and test it yourself.
Here is your JSON converted to POJO classes with getters and setters for use with GSON:
-----------------------------------com.example.Ds.java-----------------------------------
package com.example;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
publicclassDs {
@SerializedName("Table1")
@ExposeprivateList<Table1> table1 = newArrayList<Table1>();
/**
*
* @return
* The table1
*/publicList<Table1> getTable1() {
return table1;
}
/**
*
* @paramtable1
* The Table1
*/publicvoidsetTable1(List<Table1> table1) {
this.table1 = table1;
}
}
-----------------------------------com.example.Response.java-----------------------------------
package com.example;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
publicclassResponse {
@SerializedName("Status")
@ExposeprivateBoolean status;
@SerializedName("Message")
@ExposeprivateString message;
@SerializedName("ds")
@ExposeprivateDs ds;
/**
*
* @return
* The status
*/publicBooleangetStatus() {
return status;
}
/**
*
* @paramstatus
* The Status
*/publicvoidsetStatus(Boolean status) {
this.status = status;
}
/**
*
* @return
* The message
*/publicStringgetMessage() {
return message;
}
/**
*
* @parammessage
* The Message
*/publicvoidsetMessage(String message) {
this.message = message;
}
/**
*
* @return
* The ds
*/publicDsgetDs() {
return ds;
}
/**
*
* @paramds
* The ds
*/publicvoidsetDs(Ds ds) {
this.ds = ds;
}
}
-----------------------------------com.example.Table1.java-----------------------------------
package com.example;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
publicclassTable1 {
@SerializedName("CustomerId")
@ExposeprivateString customerId;
@SerializedName("CustomerName")
@ExposeprivateString customerName;
@SerializedName("CustomerNo")
@ExposeprivateString customerNo;
@SerializedName("CustomerUrl")
@ExposeprivateString customerUrl;
@SerializedName("IsActive")
@ExposeprivateBoolean isActive;
/**
*
* @return
* The customerId
*/publicStringgetCustomerId() {
return customerId;
}
/**
*
* @paramcustomerId
* The CustomerId
*/publicvoidsetCustomerId(String customerId) {
this.customerId = customerId;
}
/**
*
* @return
* The customerName
*/publicStringgetCustomerName() {
return customerName;
}
/**
*
* @paramcustomerName
* The CustomerName
*/publicvoidsetCustomerName(String customerName) {
this.customerName = customerName;
}
/**
*
* @return
* The customerNo
*/publicStringgetCustomerNo() {
return customerNo;
}
/**
*
* @paramcustomerNo
* The CustomerNo
*/publicvoidsetCustomerNo(String customerNo) {
this.customerNo = customerNo;
}
/**
*
* @return
* The customerUrl
*/publicStringgetCustomerUrl() {
return customerUrl;
}
/**
*
* @paramcustomerUrl
* The CustomerUrl
*/publicvoidsetCustomerUrl(String customerUrl) {
this.customerUrl = customerUrl;
}
/**
*
* @return
* The isActive
*/publicBooleangetIsActive() {
return isActive;
}
/**
*
* @paramisActive
* The IsActive
*/publicvoidsetIsActive(Boolean isActive) {
this.isActive = isActive;
}
}
To use this with Realm you want to extends RealmObject
the class(es) you intend to store in Realm and add a @PrimaryKey
annotation to one of the fields in each class that are unique. For example customerId
might be a candidate in the Table1 class. And if any of the classes that extends RealmObject
contains a List<Foo>
you must swap it with RealmList<Foo>
, and Foo
must extends RealmObject
as well. And if you use RealmList you must add a TypeAdapter to GSON so it knows how to handle it - here is one I wrote that takes a generic T.
Post a Comment for "Problems Organizing Complex Json Response With Realm"