gpt4 book ai didi

android - 如何将记录从 sqlite 转换为 Jsonobject?

转载 作者:行者123 更新时间:2023-12-02 02:08:47 25 4
gpt4 key购买 nike

这可能是愚蠢的问题,但我已经尽力了,但我不能在这里做,让我解释一下我的问题,我已从 sqlite 检索所有记录并使用 Gson 库进行转换,但它转换为 jsonarray 而不是 jsonobject,但我需要 jsonobject发送到服务器我该怎么做这里让我发布我的代码:

这是我检索记录的数据库代码:

*public List<Model_Account> toServer() {
String countQuery = " SELECT * FROM " + Model_Account.Accunt_Table;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
List<Model_Account> listobj = new ArrayList<Model_Account>();
if (cursor.moveToFirst()) {
do {
Model_Account modelobj = new Model_Account();
modelobj.setCompany_group(cursor.getString(cursor.getColumnIndex(Model_Account.company_groups)));
modelobj.setParent_company(cursor.getString(cursor.getColumnIndex(Model_Account.Parent_company)));
modelobj.setCompany_name_id(cursor.getInt(cursor.getColumnIndex(Model_Account.companyname_id)));
modelobj.setCompany_type(cursor.getInt(cursor.getColumnIndex(Model_Account.Company_type)));
modelobj.setAddrss_line1(cursor.getString(cursor.getColumnIndex(Model_Account.Address_line1)));
modelobj.setCityid(cursor.getInt(cursor.getColumnIndex(Model_Account.CityID)));
modelobj.setPincode(cursor.getString(cursor.getColumnIndex(Model_Account.Pincode)));
modelobj.setLandline1(cursor.getString(cursor.getColumnIndex(Model_Account.Landline1)));
modelobj.setUrl(cursor.getString(cursor.getColumnIndex(Model_Account.Url)));
modelobj.setEmailid(cursor.getString(cursor.getColumnIndex(Model_Account.Email_id)));
modelobj.setIndustryid(cursor.getInt(cursor.getColumnIndex(Model_Account.IndustryID)));
modelobj.setAcm_id(cursor.getInt(cursor.getColumnIndex(Model_Account.Account_managerid)));
modelobj.setRegionID(cursor.getInt(cursor.getColumnIndex(Model_Account.regionid)));
modelobj.setMulti_location(cursor.getInt(cursor.getColumnIndex(Model_Account.mutilocationid)));
modelobj.setStateid(cursor.getInt(cursor.getColumnIndex(Model_Account.State_id)));
modelobj.setAddrss_line2(cursor.getString(cursor.getColumnIndex(Model_Account.Address_line2)));
modelobj.setAddrss_line3(cursor.getString(cursor.getColumnIndex(Model_Account.Address_line3)));
modelobj.setLandline2(cursor.getString(cursor.getColumnIndex(Model_Account.Landline2)));
listobj.add(modelobj);

}
while (cursor.moveToNext());
}


return listobj;


}

这是我尝试转换为 jsonobject 的代码:

    listobj = account_sf_db.list();
Gson gson1=new Gson();
String s=gson1.toJson(listobj);

AM 变成这样了

[{"AddressLine1":"purasai","AddressLine2":"otteri","AddressLine3":"t nagar","CompanyGroup":"yogangroups","CompanyName":"yogan inc","IsIndividual":1,"IsMulitLocation":1,"LandLine1":"landline1","LandLine2":"Landline2","PinCode":"pincode","WebSiteContactEmailID":"vbbb","WebsiteURL":"ghb","company_name_id":68,"AccountManagerID":185,"CityID":165,"IndustryID":4,"RegionID":24,"StateID":129}]

但我必须这样:

{"AccountName":"Example"}

最佳答案

@Yog 您可能不想向 gson1 提供 listobj(这是在游标循环中构建的 Model_Account 对象的列表),而是希望提供单独的 Model_Account 对象。我不知道提供哪个 Model_Account 对象可以满足您的应用程序要求。也许您需要缩小 SQL 查询范围以获取您想要转换为 JSON 格式的唯一 Model_Account 对象。

关于android - 如何将记录从 sqlite 转换为 Jsonobject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35625298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com