问题描述
我想从我的SQLite有JSON文件并将其保存在asset.myJson.json。
以下是我的codeS,我希望看到导致myJson.json。请帮助我如何将数据导入myJson.json。
私人JSONArray jsongetResult(){
SQLiteDatabase数据库= openOrCreateDatabase(ORCL,MODE_PRIVATE,NULL);
光标光标= database.rawQuery(选择编号,名称,数量,价格从购物车中;,NULL); JSONArray的resultSet =新JSONArray(); cursor.moveToFirst();
而(cursor.isAfterLast()== FALSE){ INT totalColumn = cursor.getColumnCount();
JSONObject的rowObject =新的JSONObject(); 的for(int i = 0; I< totalColumn;我++)
{
如果(cursor.getColumnName(ⅰ)!= NULL)
{ 尝试
{ 如果(cursor.getString(ⅰ)!= NULL)
{
Log.d(TAG_NAME,cursor.getString(I));
rowObject.put(cursor.getColumnName(ⅰ),cursor.getString(ⅰ));
}
其他
{
rowObject.put(cursor.getColumnName(i)中,);
}
}
赶上(例外五)
{
Log.d(TAG_NAME,e.getMessage());
}
} } resultSet.put(rowObject); }
cursor.close();
Log.d(TAG_NAME,resultSet.toString());
返回的resultSet;}
把这个线你jsongetResult()函数
文件f =新的文件(路径);
FOS的FileOutputStream =新的FileOutputStream(F,真);
PrintStream的PS =新的PrintStream(FOS);
pa.append(resultSet.toString());
这是我如何JSON转换我的数据
公共字符串getAllDataAndGenerateJSON()抛出JSONException,FileNotFoundException异常{ 查询字符串=选择+姓名+,+地址+,+ CITY +,
+ CONTACTNO +,+ AVAILABLE +,+类别
+从CONTACT_LIST
光标C = database.rawQuery(查询,NULL);
c.moveToFirst();
JSONObject的根=新的JSONObject();
JSONArray ContactArray =新JSONArray();
文件f =新的文件(Environment.getExternalStorageDirectory()
+/ContactDetail.txt);
FOS的FileOutputStream =新的FileOutputStream(F,真);
PrintStream的PS =新的PrintStream(FOS);
INT I = 0;
而(!c.isAfterLast()){
JSONObject的接触=新的JSONObject();
尝试{
contact.put(名,c.getString(c.getColumnIndex(名)));
contact.put(地址,c.getString(c.getColumnIndex(地址)));
contact.put(城市,c.getString(c.getColumnIndex(CITY)));
contact.put(ContactNumber,c.getString(c.getColumnIndex(CONTACTNO)));
contact.put(可用,c.getString(c.getColumnIndex(可选)));
contact.put(类别,c.getString(c.getColumnIndex(类别))); c.moveToNext(); ContactArray.put(I,接触);
我++; }赶上(JSONException E){ e.printStackTrace();
} }
Root.put(CONTACTDETAILS,ContactArray);
ps.append(Root.toString());
返回Root.toString();
}
i want to have json file from my sqlite and save it in asset.myJson.json.following is my codes,i want to see result in myJson.json. please help me how to import data to myJson.json.
private JSONArray jsongetResult(){
SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
Cursor cursor = database.rawQuery("SELECT id,title,qty,price FROM CART;", null);
JSONArray resultSet = new JSONArray();
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
int totalColumn = cursor.getColumnCount();
JSONObject rowObject = new JSONObject();
for( int i=0 ; i< totalColumn ; i++ )
{
if( cursor.getColumnName(i) != null )
{
try
{
if( cursor.getString(i) != null )
{
Log.d("TAG_NAME", cursor.getString(i) );
rowObject.put(cursor.getColumnName(i) , cursor.getString(i) );
}
else
{
rowObject.put( cursor.getColumnName(i) , "" );
}
}
catch( Exception e )
{
Log.d("TAG_NAME", e.getMessage() );
}
}
}
resultSet.put(rowObject);
}
cursor.close();
Log.d("TAG_NAME", resultSet.toString() );
return resultSet;
}
put this lines in your jsongetResult() function
File f = new File("your path"); FileOutputStream fos = new FileOutputStream(f,true); PrintStream ps = new PrintStream(fos);
pa.append(resultSet.toString());
this is how i have converted my data in json
public String getAllDataAndGenerateJSON() throws JSONException, FileNotFoundException {
String query = "select " + NAME + "," + ADDRESS + "," + CITY + ","
+ CONTACTNO + "," + AVAILABLE + "," + CATEGORY
+ " from contact_list";
Cursor c = database.rawQuery(query, null);
c.moveToFirst();
JSONObject Root = new JSONObject();
JSONArray ContactArray = new JSONArray();
File f = new File(Environment.getExternalStorageDirectory()
+ "/ContactDetail.txt");
FileOutputStream fos = new FileOutputStream(f,true);
PrintStream ps = new PrintStream(fos);
int i = 0;
while (!c.isAfterLast()) {
JSONObject contact = new JSONObject();
try {
contact.put("Name", c.getString(c.getColumnIndex(NAME)));
contact.put("Address", c.getString(c.getColumnIndex(ADDRESS)));
contact.put("City", c.getString(c.getColumnIndex(CITY)));
contact.put("ContactNumber", c.getString(c.getColumnIndex(CONTACTNO)));
contact.put("Available", c.getString(c.getColumnIndex(AVAILABLE)));
contact.put("Category", c.getString(c.getColumnIndex(CATEGORY)));
c.moveToNext();
ContactArray.put(i, contact);
i++;
} catch (JSONException e) {
e.printStackTrace();
}
}
Root.put("CONTACTDETAILS", ContactArray);
ps.append(Root.toString());
return Root.toString();
}
这篇关于针对Android源码创建的Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!