这可能是愚蠢的问题,但我努力了我不能在这里让我解释我的问题我已经从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"}
答案 0 :(得分:0)
@Yog而不是向gson1提供listobj(它是在游标循环中构建的Model_Account对象的列表),您可能希望提供单独的Model_Account对象。我不知道提供哪个Model_Account对象会满足您的应用程序要求。也许你需要缩小你的SQL查询以获得你想要转换为JSON格式的唯一Model_Account对象。