jeudi 25 février 2016

How to convert Records From sqlite to Jsonobject?

This may be Dumb question but i have tried hard i couldn't do here let me explain my problem i have retrieve all records from sqlite and converted using Gson library but it is converting as jsonarray not as jsonobject but i need jsonobject to send to the server how can i do this here let me post my code:

This is the db code where i retrieve records:

*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;


    }

Here is the code where i try to convert to jsonobject:

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

AM getting like this

[{"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}]

But i must get like this:

{"AccountName":"Example"}

Aucun commentaire:

Enregistrer un commentaire