2016-10-01 29 views
0

我正嘗試使用我的android應用程序的領域。我嘗試尋找一些關於領域的簡單例子。我能夠設置項目的領域,現在想開始實施它,但面臨問題根據我的JSON響應生成分類。用Realm組織複雜的json響應的問題

以下是我將使用

{ 
    "Status":true, 
    "Message":"Success", 
    "ds":{ 
      "Table1":[{ 
        "CustomerId":"1", 
        "CustomerName":"TestUser", 
        "CustomerNo":"100001", 
        "CustomerUrl":"http://abc-001-site10.itempurl.com", 
        "IsActive":true}]}} 

我需要生成RealmObject這個POJO類JSON的例子,我怎麼能生成它,因爲我完全難倒。

我也將與此一起使用GSON。

需要一些指導。

+0

Realm對象應該是「Table1」的內容對象 – EpicPandaForce

+0

對於Realm/GSON,看看https://realm.io/docs/java/latest/#gson – geisshirt

回答

1

這個工作有一個很好的工具 - 去http://www.jsonschema2pojo.org/並自己測試一下。

這裏是你的JSON轉換成POJO類的getter和setter方法爲使用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; 
} 

} 

要與境界要extends RealmObject類(ES)你打算在境界儲存,並加入使用一個@PrimaryKey註釋到每個類中唯一的一個字段。例如customerId可能是Table1類中的候選人。如果extends RealmObject包含List<Foo>的任何類別,則必須將其與RealmList<Foo>Foo必須互換爲extends RealmObject。如果您使用RealmList,則必須爲GSON添加TypeAdapter,以便它知道如何處理它 - here is one我寫道需要一個通用T.