I need to retrieve data (two words - Mega building) from Firebase correctly. However, "Mega building" is retrieved as "Megabuilding". As seen, it happens without space between two words. Is there any solution for this to draw data like "Mega building"? or Can it be done without space, is there an alternative solution when retrieving JSON object data?
Firebase node:
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”
My function:
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();
}
}
First output:
My_basket:[{cat=Titan Tech, info=Mega Building, orderid=Ref_1,}, {cat=Tech, info=Android, orderid=Ref_2,}]
Second output (ı want to retrieve like “Mega Building”. Problem line is here)
Info_: MegaBuilding
Third output
org.json.JSONException: Unterminated object at character 23 of etc
CodePudding user response:
First of all, you need to mark the spaces where there are spaces.
json = new JSONObject(String.valueOf(value).replaceAll(" ","-"));
Output : Mega-Building
Then just replace the marker you created with spaces again. That's it.
info_ = info_.replace("-"," ");
Output : Mega Building
