从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代码,它获取数据并保存到DB
@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
中恢复数据的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+");");
}
当我把结果设置3放在javascript中时。我正在接受&#34; \&#34;所有字符串旁边。
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!\"}]"]"
答案 0 :(得分:0)
如果你想从json中删除反斜杠,试试这个:
String newJsonString = jsonString.replaceAll("\\\\", "");
答案 1 :(得分:0)
以下代码为我工作
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+");");
}
答案 2 :(得分:0)
我已尝试使用以下代码,希望它可以帮助您
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!"}]