我需要從 Firebase 正確檢索資料(兩個詞 - Mega building)。但是, “Mega building”被檢索為“Megabuilding”。正如所見,它發生在兩個單詞之間沒有空格。有什么解決方案可以繪制像“巨型建筑”這樣的資料嗎?或者可以在沒有空間的情況下完成,在檢索 JSON 物件資料時是否有替代解決方案?
Firebase 節點:
My node;
Pro_:
Basket:
ID_1:
cat: “ Titan Tech”
info:”Mega Building”
orderid:”Ref_1”
ID_2:
cat: “Tech”
info:”Android”
orderid:”Ref_2”
name:”Mike”
我的功能:
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
Map<String, Object> map1 = (Map)bundle.getSerializable(“basket”);
//First output
System.out.println(“My_basket:” map1.values());
for (Object value : map1.values()) {
JSONObject json = null;
try {
json = new JSONObject(String.valueOf(value).replaceAll(" ",""));
String cat_ = json.getString(“cat”);
String info_ = json.getString(“info”);
String orderid_ = json.getString(“orderid”);
//Second output
System.out.println(“Info_:” info_);
} catch (JSONException e) {
//Third output
e.printStackTrace();
}
}
第一個輸出:
My_basket:[{cat=Titan Tech, info=Mega Building, orderid=Ref_1,}, {cat=Tech, info=Android, orderid=Ref_2,}]
第二個輸出(我想像“Mega Building”一樣檢索。問題行在這里)
Info_: MegaBuilding
第三個輸出
org.json.JSONException: Unterminated object at character 23 of etc
uj5u.com熱心網友回復:
首先,您需要標記有空格的空格。
json = new JSONObject(String.valueOf(value).replaceAll(" ","-"));
輸出:巨型建筑
然后再次用空格替換您創建的標記。而已。
info_ = info_.replace("-"," ");
輸出:巨型建筑
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/412482.html
標籤:
