当前位置: 动力学知识库 > 问答 > 编程问答 >

android - 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 implementing it but facing problems to generate classed in accordance to my json response.

following is an example of json I will be using

{

"Status":true,

"Message":"Success",

"ds":{

"Table1":[{

"CustomerId":"1",

"CustomerName":"TestUser",

"CustomerNo":"100001",

"CustomerUrl":"http://abc-001-site10.itempurl.com",

"IsActive":true}]}}

I need to generate POJO class for this with RealmObject, how can I generate it as I am completely stumped.

I also will be using GSON along with this.

Need some guidance.

网友答案:

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")
public class Ds {

@SerializedName("Table1")
@Expose
private List<Table1> table1 = new ArrayList<Table1>();

/**
* 
* @return
* The table1
*/
public List<Table1> getTable1() {
return table1;
}

/**
* 
* @param table1
* The Table1
*/
public void setTable1(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")
public class Response {

@SerializedName("Status")
@Expose
private Boolean status;
@SerializedName("Message")
@Expose
private String message;
@SerializedName("ds")
@Expose
private Ds ds;

/**
* 
* @return
* The status
*/
public Boolean getStatus() {
return status;
}

/**
* 
* @param status
* The Status
*/
public void setStatus(Boolean status) {
this.status = status;
}

/**
* 
* @return
* The message
*/
public String getMessage() {
return message;
}

/**
* 
* @param message
* The Message
*/
public void setMessage(String message) {
this.message = message;
}

/**
* 
* @return
* The ds
*/
public Ds getDs() {
return ds;
}

/**
* 
* @param ds
* The ds
*/
public void setDs(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")
public class Table1 {

@SerializedName("CustomerId")
@Expose
private String customerId;
@SerializedName("CustomerName")
@Expose
private String customerName;
@SerializedName("CustomerNo")
@Expose
private String customerNo;
@SerializedName("CustomerUrl")
@Expose
private String customerUrl;
@SerializedName("IsActive")
@Expose
private Boolean isActive;

/**
* 
* @return
* The customerId
*/
public String getCustomerId() {
return customerId;
}

/**
* 
* @param customerId
* The CustomerId
*/
public void setCustomerId(String customerId) {
this.customerId = customerId;
}

/**
* 
* @return
* The customerName
*/
public String getCustomerName() {
return customerName;
}

/**
* 
* @param customerName
* The CustomerName
*/
public void setCustomerName(String customerName) {
this.customerName = customerName;
}

/**
* 
* @return
* The customerNo
*/
public String getCustomerNo() {
return customerNo;
}

/**
* 
* @param customerNo
* The CustomerNo
*/
public void setCustomerNo(String customerNo) {
this.customerNo = customerNo;
}

/**
* 
* @return
* The customerUrl
*/
public String getCustomerUrl() {
return customerUrl;
}

/**
* 
* @param customerUrl
* The CustomerUrl
*/
public void setCustomerUrl(String customerUrl) {
this.customerUrl = customerUrl;
}

/**
* 
* @return
* The isActive
*/
public Boolean getIsActive() {
return isActive;
}

/**
* 
* @param isActive
* The IsActive
*/
public void setIsActive(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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: