如何删除JSON中的双引号?

时间:2015-12-15 22:38:47

标签: android json sqlite

从数据库中我以下列方式获得JSON

 String DB_PATH = "/data/data/com.example.apps/databases/";
 String myPath = DB_PATH + DBHelper.DATABASE_NAME;// Set path to your database
 String myTable = DBHelper.TABLE_CONTACT;//Set name of your table
 SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    String searchQuery = "SELECT  * FROM " + myTable;
    Cursor cursor = myDataBase.rawQuery(searchQuery, null );

    JSONArray json = 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());
                }
            }
        }
        json.put(rowObject);
        cursor.moveToNext();
    }
    cursor.close();
    Log.d("TAG_NAME_ALL", json.toString());
    return json.toString();

JSON正确,除了一个值 latlng

我得到了 - &#34; latlng&#34;:&#34; [12.3456789,98.7654321]&#34;

需要获得 - &#34; latlng&#34;:[12.3456789,98.7654321]

如何摆脱双引号?

1 个答案:

答案 0 :(得分:3)

如果您只想快速而肮脏地将该响应转换为double值,则可以执行以下操作:

String s = "[12.3456789,98.7654321]";
String trimmed = s.substring(1, s.length()-1);
String[] parts = trimmed.split(",");
double lat = Double.parseDouble(parts[0]);
double lng = Double.parseDouble(parts[1]);
Log.d("latlng", "lat="+lat+"  lng="+lng);

它不漂亮,但它确实有效。