在Android中从sqlite创建Json

在Android中从sqlite创建Json,第1张

概述我想从我的sqlite获取json文件并将其保存在asset.myJson.json中.以下是我的代码,我想在myJson.json中看到结果.请帮我如何将数据导入myJson.json.privateJSONArrayjsongetResult(){SQLiteDatabasedatabase=openOrCreateDatabase("ORCL",MODE_PRIVATE,null);Cur

我想从我的sqlite获取Json文件并将其保存在asset.myJson.Json中.
以下是我的代码,我想在myJson.Json中看到结果.请帮我如何将数据导入myJson.Json.
@H_403_3@

@H_403_3@

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

解决方法:@H_403_3@

把这些行放在你的JsongetResult()函数中@H_403_3@

文件f =新文件(“你的路径”);
        fileOutputStream fos = new fileOutputStream(f,true);
        PrintStream ps = new PrintStream(fos);@H_403_3@

pa.append(resultSet.toString());@H_403_3@

这就是我在Json中转换数据的方式@H_403_3@

@H_403_3@

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中从sqlite创建Json全部内容,希望文章能够帮你解决在Android中从sqlite创建Json所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:http://outofmemory.cn/web/1115604.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存