本文介绍了使用POJO类的GreenDao中的自定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的示例JSON:
This is my sample JSON:
{
"open":true,
"total_products":100,
"product":[
{
"p_id":1,
"price":"5.00",
"name":"blah one"
},
{
"p_id":2,
"price":"15.00",
"name":"blah two"
},
...
]
}
这是我的POJO课:
@Entity(nameInDb = "products")
public class ProductsPOJO {
@SerializedName("open")
@Property(nameInDb = "open")
private boolean open;
@SerializedName("total_products")
@Property(nameInDb = "total_products")
private Long total_products;
@Convert(converter = ProductConverter.class, columnType = String.class)
@SerializedName("product")
@Property(nameInDb = "product")
private Product product;
public static class productConverter implements PropertyConverter<Product, String> {
//What shoudl I write in convert part?!
@Override
public Product convertToEntityProperty(String databaseValue) {
if (databaseValue == null) {
return null;
}
for (Product p : Product.values()) {
if (p.id == databaseValue) {
return p;
}
}
return Product.DEFAULT;
}
@Override
public String convertToDatabaseValue(Product entityProperty) {
return entityProperty == null ? null : entityProperty.;
}
//
/*@Override
public Product convertToEntityProperty(String databaseValue) {
return Product.valueOf(databaseValue);
}
@Override
public String convertToDatabaseValue(Product entityProperty) {
return entityProperty.name();
}*/
}
public static class Product{
@Id
@SerializedName("p_id")
private Long p_id;
@SerializedName("price")
@Property(nameInDb = "price")
private String price;
@SerializedName("name")
@Property(nameInDb = "name")
private String name;
//Getters & Setters
}
public Product getProduct() {
return product;
}
public void setProduct(Product data) {
this.product = product;
}
//open & total_products Getters & Setters
}
但是我不知道该为productConverter
写什么.
另一方面,Product
类中的字段具有多种类型. String
和Integer
.
我读了这些: https://github.com/greenrobot/greenDAO/blob/V3.1.1/examples/DaoExample/src/main/java/org/greenrobot/greendao/example/Note.java#L26-L27
But I don't know what should I write for productConverter
.
In other hand, fields in Product
class have multiple types. String
and Integer
.
I read these:https://github.com/greenrobot/greenDAO/blob/V3.1.1/examples/DaoExample/src/main/java/org/greenrobot/greendao/example/Note.java#L26-L27
http://greenrobot.org/greendao/documentation/custom-types/
推荐答案
是因为
应该做这样的事情:
JsonParser parser = new JsonParser();
JsonObject rootObject = parser.parse(JSON_STRING).getAsJsonObject();
//You can get the "open" and "total_products" here too.//
JsonElement productElement = rootObject.get("product");
Gson gson = new Gson();
List<Product> productList = new ArrayList<>();
//Check if "data" element is an array or an object and parse accordingly...
if (productElement.isJsonObject()) {
//The returned list has only 1 element
Product p = gson.fromJson(productElement, Product.class);
productList.add(p);
}
else if (productElement.isJsonArray()) {
//The returned list has >1 elements
Type productListType = new TypeToken<List<Product>>() {}.getType();
productList = gson.fromJson(productElement, productListType);
}
[来源: https://stackoverflow.com/a/16656096/421467 ]
这篇关于使用POJO类的GreenDao中的自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!