//我正在将sqlite数据转换为json格式
试
{
SQLiteDatabase db;
String dbDir= Environment.getExternalStorageDirectory()+"/"+getResources().getString(R.string.folderName);;
db=getActivity().openOrCreateDatabase(dbDir + "/" + getResources().getString(R.string.dbName), Context.MODE_PRIVATE, null);
Cursor c = db.rawQuery("select * from products", null);
Log.e("fetch_category", "" + c.getCount());
if (c.getCount() == 0)
{
}
else
{
c.moveToFirst();
JSONObject rowObject = new JSONObject();
do
{
int totalColumn = c.getColumnCount();
for( int i=0 ; i< totalColumn ; i++ )
{
if( c.getColumnName(i) != null )
{
try
{
if( c.getString(i) != null )
{
Log.d("TAG_NAME", c.getString(i) );
rowObject.put(c.getColumnName(i) , c.getString(i) );
}
else
{
rowObject.put( c.getColumnName(i) , "" );
}
}
catch( Exception e )
{
Log.d("TAG_NAME", e.getMessage() );
}
}
}
// billsModel=new Bills_model();
resultSet.put(rowObject);
Log.d("resultset",resultSet.toString());
}
while (c.moveToNext());
}
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
// out put就像这样
[{&#34;键&#34;&#34;值&#34;},{&#34;键&#34;&#34;值&#34;} ....]
//但我想这样可能
{&#34;产品&#34; [{&#34;键&#34;&#34;值&#34;},{&#34;键&#34;&#34;值&#34 ;} ....]}
答案 0 :(得分:0)
您可以使用杰克逊图书馆 -
创建响应类。
使用@JsonProperty注释 -
@JsonProperty(&#34;产品&#34) private String jsonArray;
//吸气剂
//设定器
答案 1 :(得分:0)
//执行以下操作
JSONObject jobj = new JSONObject();
//然后将所有数组放在json对象中,它将给出欲望结果
jobj.put(&#34;产品&#34;,resultSet.put(rowObject));