我使用greenDAO作为我的ORM工具,在使用GSON解析JSON后将数据保存到表中时遇到了一些问题。 在将其保存到DB之前,我获得了完整的List。 在这里,我附上了代码片段。请看一下并给我你的建议。
MainGenerator.java
public class MainGenerator {
private static final String PROJECT_DIR = System.getProperty("user.dir");
public static void main(String[] args) {
Schema schema = new Schema(1, "com.example.cisystem3.mobiledeliveryndispatch.dao");
schema.enableKeepSectionsByDefault();
addTables(schema);
try {
new DaoGenerator().generateAll(schema, PROJECT_DIR + "\\app\\src\\main\\java");
} catch (Exception e) {
e.printStackTrace();
}
}
private static void addTables(final Schema schema) {
Entity setting = addSetting(schema);
...
Entity item = addItems(schema);
...
}
private static Entity addItems(final Schema schema) {
Entity item = schema.addEntity("ItemDetails");
item.addStringProperty("itemNum");
item.addStringProperty("itemDesc");
item.addStringProperty("uom");
return item;
}
GreenDAO使用MainGenerator ItemDetails,ItemDetailsDAO生成了两个自动文件。 现在从WcfService获取数据时,我创建了一个额外的类ITEMDETAIL.java,它扩展了ItemDetails类,如下所示
ItemDetails.java
public class ItemDetails {
private String itemNum;
private String itemDesc;
private String uom;
// KEEP FIELDS - put your custom fields here
// KEEP FIELDS END
public ItemDetails() {
}
public ItemDetails(String itemNum, String itemDesc, String uom) {
this.itemNum = itemNum;
this.itemDesc = itemDesc;
this.uom = uom;
}
public String getItemNum() {
return itemNum;
}
public void setItemNum(String itemNum) {
this.itemNum = itemNum;
}
public String getItemDesc() {
return itemDesc;
}
public void setItemDesc(String itemDesc) {
this.itemDesc = itemDesc;
}
public String getUom() {
return uom;
}
public void setUom(String uom) {
this.uom = uom;
}
}
ITEMDETAIL.java
@Generated("org.jsonschema2pojo")
public class ITEMDETAIL extends ItemDetails {
@SerializedName("ITEMDESC")
@Expose
private String iTEMDESC;
@SerializedName("ITEMNO")
@Expose
private String iTEMNO;
@SerializedName("LOCATION")
@Expose
private String lOCATION;
@SerializedName("PRICELIST")
@Expose
private String pRICELIST;
@SerializedName("QTYONHAND")
@Expose
private Integer qTYONHAND;
@SerializedName("UNITPRICE")
@Expose
private Double uNITPRICE;
@SerializedName("UOM")
@Expose
private String uOM;
//Getters and Setters
}
使用GSON解析JSON如下并创建List
if (response != null) {
JSONObject jsonObject = response.getJSONObject(Constants.GetProductionResult);
JSONArray jsonArray = jsonObject.getJSONArray(Constants.GetItemDetails);
Gson gson = new Gson();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
ITEMDETAIL itemdetail = gson.fromJson(jsonObject1.toString(), ITEMDETAIL.class);
posts.add(itemdetail);
}
}
创建列表后,我想将其保存在表中,如下所示
@Override
public synchronized void insertItemDetails(ArrayList<ITEMDETAIL> itemList) {
try {
if (itemList != null) {
//Values came here
for (ItemDetails item : itemList) {
openWritableDb();
ItemDetailsDao itemDetailsDao = daoSession.getItemDetailsDao();
itemDetailsDao.insert(item);
daoSession.clear();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
通过更改for循环,可以插入:
for (ITEMDETAIL item : itemList) {
ItemDetails details = new ItemDetails();
details.setItemDesc(item.getITEMDESC());
details.setItemNum(item.getITEMNO());
details.setUom(item.getUOM());
openWritableDb();
ItemDetailsDao itemDetailsDao = daoSession.getItemDetailsDao();
itemDetailsDao.insert(details);
daoSession.clear();
}