从 SQLite 中检索 JSON 数据时,反斜杠显示在数组中

Posted

技术标签:

【中文标题】从 SQLite 中检索 JSON 数据时,反斜杠显示在数组中【英文标题】:When retrieving JSON data from SQLite back slash is showing up in the array 【发布时间】:2018-08-19 16:31:27 【问题描述】:

从 SQLite 检索 JSON 数据时,转义字符“\”显示在数组中。最初,我在 Web-view javascript 中创建了 JSON 数组,然后将其发送到 android java 以保存在 DB 中。

下面是我发送到 Java 的 JSON 字符串。

adtMf = ["companyId":"12004","employeeId":"11345","employeeName":"jaseem","employeeDepartment":"SalesExecutive","employeePosition":"Staff","itemMeasurementNotes":"Notes here","itemMeasurementOwner":"someone","Items":"Shirt?ShirtPrice:1.1!ShirtQty:1.1!ShirtLength:2.12!ShirtShoulder:1.12!ShirtSleeveL:3.12!ShirtSleeveW:5.14!ShirtSleeveCuff:2.12!ShirtChest:3.12!ShirtBelly:4.12!ShirtSeat:5.12!ShirtCollar:5.12!ShirtSlideL1:4.1!ShirtSlideL2:4.12!ShirtSlideL3:2.4!"]
android.addMeasurement(JSON.stringify(adtMf));

下面是我的android代码,它获取数据并保存到数据库

 @JavascriptInterface
public void updateMeasurement(String response) 
    db.addADTMeasurement(response.toString());


public void addADTMeasurement(String ADTMeasurment) 
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_ADTMEASUREMENT, ADTMeasurment); 
    // Inserting Row
    long id = db.insert(TABLE_MYMEASUREMENT, null, values);
    Log.d(TAG, "New Measurement into sqlite: " + id);

从 SQLite 检索数据后,JSON 数组包含“\”。我该如何删除它?添加到 SQLite 时,我的代码没有添加 "\" 吗? .由于某些奇怪的原因,Android 设备管理器文件资源管理器无法正常工作,因此我无法确认数据是如何存储在 DB 中的。

以下是用于从 DB 中检索数据的 android 代码

 public Cursor fetchADTMeasurement() 
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor mCursor = db.query(TABLE_MYMEASUREMENT, new String[] KEY_ADTMEASUREMENT,
            null, null, null, null, null);
    if (mCursor != null) 
        mCursor.moveToFirst();
    
    return mCursor;


 Cursor mycursor2 = db.fetchADTMeasurement();
 JSONArray resultSet3 = new JSONArray();
 mycursor2.moveToFirst();
 String id = mycursor2.getString( mycursor2.getColumnIndex("ADTMeasurement") );
 while (mycursor2.isAfterLast() == false) 
    int totalColumn = mycursor2.getColumnCount();
    JSONObject rowObject = new JSONObject();
    for (int i = 0; i < totalColumn; i++) 
        if (mycursor2.getColumnName(i) != null) 
            try 
                resultSet3.put(mycursor2.getString(i));
                 catch (Exception e)  
          
      
      mycursor2.moveToNext();
     
     mycursor2.close();
    super.onPageFinished(view, url);
    if (url.equals("file:///android_asset/myMeasurments_a.html")) 
                   myWebView.loadUrl("javascript:start("+resultSet3+");");
                

当我将 resultSet3 放入 javascript 中时。我在所有字符串旁边都得到了“\”。

function start(d)
    adtMf = d.slice();
    console.log(JSON.stringify(adtMf));


I/chromium: [INFO:CONSOLE(42)] "["[\"companyId\":12004,\"employeeId\":\"11345\",\"employeeName\":\"jaseem\",\"employeeDepartment\":\"SalesExecutive\",\"employeePosition\":\"Staff\",\"itemMeasurementNotes\":\"Notes here\",\"itemMeasurementOwner\":\"someone\",\"Items\":\"Shirt?ShirtPrice:1.1!ShirtQty:1.1!ShirtLength:2.12!ShirtShoulder:1.12!ShirtSleeveL:3.12!ShirtSleeveW:5.14!ShirtSleeveCuff:2.12!ShirtChest:3.12!ShirtBelly:4.12!ShirtSeat:5.12!ShirtCollar:5.12!ShirtSlideL1:4.1!ShirtSlideL2:4.12!ShirtSlideL3:2.4!\"]"]"

【问题讨论】:

【参考方案1】:

如果你想从 json 中删除反斜杠,试试这个:

   String newJsonString = jsonString.replaceAll("\\\\", "");

【讨论】:

它不起作用。使用我的 json 数组添加上述代码时,它显示为红色。 嗨@user1846348,我已经发布了答案,请查看link【参考方案2】:

下面的代码对我有用

Cursor mycursor2 = db.fetchADTMeasurement();
mycursor2.moveToFirst();
String resultSet3 = mycursor2.getString(0);
mycursor2.close();

super.onPageFinished(view, url);
if (url.equals("file:///android_asset/myMeasurments_a.html")) 
    myWebView.loadUrl("javascript:start("+resultSet3+");");

【讨论】:

【参考方案3】:

我已经用下面的代码试过了,希望对你有帮助

String jsonString="[\\\"companyId\\\":12004,\\\"employeeId\\\":\\\"11345\\\",\\\"employeeName\\\":\\\"jaseem\\\",\\\"employeeDepartment\\\":\\\"SalesExecutive\\\",\\\"employeePosition\\\":\\\"Staff\\\",\\\"itemMeasurementNotes\\\":\\\"Notes here\\\",\\\"itemMeasurementOwner\\\":\\\"someone\\\",\\\"Items\\\":\\\"Shirt?ShirtPrice:1.1!ShirtQty:1.1!ShirtLength:2.12!ShirtShoulder:1.12!ShirtSleeveL:3.12!ShirtSleeveW:5.14!ShirtSleeveCuff:2.12!ShirtChest:3.12!ShirtBelly:4.12!ShirtSeat:5.12!ShirtCollar:5.12!ShirtSlideL1:4.1!ShirtSlideL2:4.12!ShirtSlideL3:2.4!\\\"]";
Log.e("jsonString=",jsonString);
String newJsonString = jsonString.replaceAll("\\\\", "");
Log.e("newJsonString=",newJsonString);

控制台日志:jsonString 是

jsonString=: [\"companyId\":12004,\"employeeId\":\"11345\",\"employeeName\":\"jaseem\",\"employeeDepartment\":\"SalesExecutive\",\"employeePosition\":\"Staff\",\"itemMeasurementNotes\":\"Notes here\",\"itemMeasurementOwner\":\"someone\",\"Items\":\"Shirt?ShirtPrice:1.1!ShirtQty:1.1!ShirtLength:2.12!ShirtShoulder:1.12!ShirtSleeveL:3.12!ShirtSleeveW:5.14!ShirtSleeveCuff:2.12!ShirtChest:3.12!ShirtBelly:4.12!ShirtSeat:5.12!ShirtCollar:5.12!ShirtSlideL1:4.1!ShirtSlideL2:4.12!ShirtSlideL3:2.4!\"]

控制台日志:newJsonString 是

newJsonString=: ["companyId":12004,"employeeId":"11345","employeeName":"jaseem","employeeDepartment":"SalesExecutive","employeePosition":"Staff","itemMeasurementNotes":"Notes here","itemMeasurementOwner":"someone","Items":"Shirt?ShirtPrice:1.1!ShirtQty:1.1!ShirtLength:2.12!ShirtShoulder:1.12!ShirtSleeveL:3.12!ShirtSleeveW:5.14!ShirtSleeveCuff:2.12!ShirtChest:3.12!ShirtBelly:4.12!ShirtSeat:5.12!ShirtCollar:5.12!ShirtSlideL1:4.1!ShirtSlideL2:4.12!ShirtSlideL3:2.4!"]

【讨论】:

以上是关于从 SQLite 中检索 JSON 数据时,反斜杠显示在数组中的主要内容,如果未能解决你的问题,请参考以下文章

从 c# json 响应中删除双反斜杠

如何去掉Json字符串中反斜杠

Json.NET在返回json序列化字符串时添加反斜杠

如何使用json_encode回显没有反斜杠的值[重复]

json_encode 添加反斜杠

如何防止 JSONKit 从 ASP.NET JSON 日期格式转义反斜杠?