0

javascript code: (It creates a json object -> convert to json string -> upload to firebase)

var data = {}
data.systemId = "21341"

// convert json to string
var dataString = JSON.stringify(data);

// push to firebase

var update = {}
update[1718964] = dataString
firebase.database().ref("/data").update(update)

Now the data in firebase console looks like:

enter image description here

Now i try to read the data in Java using Apache httpClient

HttpGet httpGet = new HttpGet(url);         
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String dataString = EntityUtils.toString(entity);

System.out.println(dataString); 
// ABOVE sysout exactly prints below with quotes included
// "{\"systemId\":\"21341\"}"

I was expecting it to print. {"systemId" : "21341"}

So why it is printing data with \ and starting ending quotes?

WHy i expect it to print it without quotes and slash is based on below code

String temp = "{\"systemId\" : \"21341\"}"
System.out.println(temp);
// prints
//{"systemId" : "21341"}

What mistake am i making ? PS: The content type of the get request is "application/json" but content received is just a string... the problem i think is in how i am handling the data in java.. but still dont know how to fix it?

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Bhuvan
  • 4,028
  • 6
  • 42
  • 84
  • `dataString` is a string. You need to parse it as an object first. – Cerbrus Aug 19 '16 at 09:27
  • Because you are reading the data as plain text instead of treating it as a JSON string. Parse that text with the `javax.json` package or another JSON library like Jackson or GSON. – dabadaba Aug 19 '16 at 09:27
  • @Cerbrus `dataString` is a string.. true.. which is intended... why did the temp printed it without quotes and slash – Bhuvan Aug 19 '16 at 09:47
  • @user2410148: The `println` doesn't render the outer "quotes" of the string, so there's no need to escape the inner quotes of the string. – Cerbrus Aug 19 '16 at 09:48
  • added a better version of the problem http://stackoverflow.com/questions/39098101/fetching-json-from-firebase-as-string-not-working-in-java – Bhuvan Aug 23 '16 at 10:11

0 Answers0