用Realm组织复杂的json响应的问题 [英] Problems organizing complex json response with Realm

查看:145
本文介绍了用Realm组织复杂的json响应的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的android应用程序的领域。我尝试寻找一些关于领域的简单例子。我能够设置项目的领域,现在想开始实施它,但面临问题根据我的JSON响应生成分类。

以下是json的一个例子我将使用

  {
状态:true,
消息:成功 ,
ds:{
Table1:[{
CustomerId:1,
CustomerName:TestUser,
CustomerNo :100001,
CustomerUrl:http://abc-001-site10.itempurl.com,
IsActive:true}]}}

我需要使用RealmObject为此生成POJO类,我完全难住时该如何生成它。



我也将使用GSON。



需要一些指导。

解决方案

这项工作有一个很好的工具 - 去 http://www.jsonschema2pojo.org/ 并自行测试。



这里是您的JSON转换为POJO类,可以使用getter和setters来使用与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
私人列表< Table1> table1 = new ArrayList< Table1>();
$ b $ ** / **
*
* @return
* table1
* /
public List< Table1> getTable1(){
return table1;
}

/ **
*
* @param table1
* 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
私有布尔状态;
@SerializedName(Message)
@Expose
private String message;
@SerializedName(ds)
@Expose
private Ds ds;
$ b $ **
*
* @return
*状态
* /
public Boolean getStatus(){
退货状态;
}

/ **
*
* @param status
*状态
* /
public void setStatus布尔状态){
this.status = status;

$ b / **
*
* @return
*消息
* /
public String getMessage() {
返回消息;
}

/ **
*
* @param message
*消息
* /
public void setMessage(字符串消息){
this.message = message;

$ b / **
*
* @return
* ds
* /
public Ds getDs() {
return ds;

$ b / **
*
* @param ds
* 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
私人字符串customerId;
@SerializedName(CustomerName)
@Expose
private String customerName;
@SerializedName(CustomerNo)
@Expose
private String customerNo;
@SerializedName(CustomerUrl)
@Expose
private String customerUrl;
@SerializedName(IsActive)
@Expose
private布尔isActive;
$ b / **
*
* @return
* customerId
* /
public String getCustomerId(){
返回customerId;

$ b $ **
*
* @param customerId
* CustomerId
* /
public void setCustomerId(字符串customerId){
this.customerId = customerId;

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

/ **
*
* @param customerName
* CustomerName
* /
public void setCustomerName字符串customerName){
this.customerName = customerName;

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

/ **
*
* @param customerNo
* CustomerNo
* /
public void setCustomerNo字符串customerNo){
this.customerNo = customerNo;

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

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

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

/ **
*
* @param isActive
* IsActive
* /
public void setIsActive布尔isActive){
this.isActive = isActive;





$ p $要使用Realm你想to 扩展RealmObject 您打算在Realm中存储的类,并将 @PrimaryKey 注释添加到其中一个每个类中的字段都是唯一的。例如 customerId 可能是Table1类中的候选人。如果任何类扩展RealmObject 的类包含 List< Foo> ,则必须将它与 RealmList< Foo> Foo 必须扩展RealmObject 。如果您使用RealmList,则必须为GSON添加TypeAdapter,以便知道如何处理它 - 这里有一个我写了一个泛型T。


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.

这篇关于用Realm组织复杂的json响应的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆