I send JSON at node : response.json(object);
and I get json at android :
String json_string = EntityUtils.toString(response.getEntity());
And json_string from android :
"{\"query\":{\"count\":4,\"created\":\"2014-11-07T15:10:16Z\",\"lang\":\"en-US\",\"diagnostics\":{\"publiclyCallable\":\"true\", ~ }
It's different from what I generally got.
character '"' and '\' is added to json string
So I'm troubling in getting the result from json string.
Is there something wrong?
Its okk, You are getting just raw json.
Do one thing cast this string in StringBuilder and again StringBuilder to string. Its remove all "\ character. Try this its works for me
Your string is just escaped, to keep special characters like double quotes. If there was no escaping, then your string would look just like that:
"{\"
So you need to indicate that some of double quotes shouldn't be treaded like end of the string.
Refer to http://docs.oracle.com/javase/tutorial/java/data/characters.html
Related
My problem is that I am getting strings where some characters are Unicode.
"fieldName": "Ac6jHguQjKKUxx6MSOpjO2kOLKPAdjStVs1pgTGNSU8\u003d"
Then I immediately send such a string to another API and the server returns me an error with a code of 500. If I use this string in postman and replace the unicode with a normal one, then the code 200 is returned from the server.
I thought there was a problem in the server, but they checked it and said that they were sending it as expected.
How do I translate Unicode?
The easiest way is to use URLDecoder. Here is an example.
String str = "Ac6jHguQjKKUxx6MSOpjO2kOLKPAdjStVs1pgTGNSU8\u003d";
String decode = URLDecoder.decode(str, "UTF-8");
System.out.println(decode);
//Ac6jHguQjKKUxx6MSOpjO2kOLKPAdjStVs1pgTGNSU8=
my json is
{[{"key1":"value1","key2":"valu2"},{“ key3":"value3","key4":"valu4”}]}
How to change the above text as follows. Thank you for helping my friends
{"travel": [{"key1":"value1","key2":"valu2"},{ key3":"value3","key4":"value4}]}
Your original String is not a valid JSON for two reasons:
1 we can see some invalid quotes
2 elements inside a json object must have keys.
so assuming that your String is correct to achieve what you need in java you can do the following:
String string="{[{\"key1\":\"value1\",\"key2\":\"valu2\"},{\" key3\":\"value3\",\"key4\":\"valu4\"}]}";
StringBuilder stringBuilder=new StringBuilder(string);
stringBuilder.insert(1,"\"travel\":");
String json=stringBuilder.toString();
You just insert your key after the first character. Hope this will help.
I try to put image saved in base64 to JSONObject. Everything is ok but when I put String to jsonobject: verificationDataPersonal.put("file_data", imageObj.getFile_data()); before every "/" i get sign "\". So in String I have value: /9j/4AAQSkZJRg... and when i put string to json I get: \/9j\/4AAQSkZJRg... There is any way to prevent this?
I think it is not a problem, maybe the first "\" is an escape sequence, isn't it?
Have you proved to convert it back from JSON to String to see if it comes back ok?
I am trying to parse the "html_instructions" string from the "steps" array at this link:
I have no trouble parsing the string, but it returns with bits of code mixed in. For Example, the parsed string of:
"html_instructions" : "Head \u003cb\u003esouthwest\u003c/b\u003e toward \u003cb\u003eCapitol Square SW\u003c/b\u003e",
Appears as:
Head<br>southwest"</br>"towards<br>...
Instead of appearing simply as:
Head southwest towards...
Is there a way i can format the string to remove the "breaks"? Any help is greatly appreciated.
You can use a regex to remove HTML tags from your content.
String htmlString = "Head<br>southwest</br>towards<br>...";
String noHtml = htmlString.replaceAll("<[^>]*>", "");
Look into answer for this question.
How to convert unicode in JavaScript?
Basically the response is in Unicode, hence the < is represented by \u003c & > by \u003e you can use String manipulation to replace these strings with appropriate charterers.
Try doing something like this
String parseResponse = response.replaceAll("\u003c","<");
This will get your string in the proper html format.
Hey, how can I read a value of a cookie?
Example:
String cs = CookieManager.getInstance().getCookie();
System.out.println("Cookies string: "+cs);
This will give me a string which has to be parsed with split on ';' and '='. Is there a "cookie string reader" or smth? Is there any other way of reading the value of only one particular cookie in the webview?
Thx!
Well, I suggest that you parse the string into an Array yourself. That would then be something along these lines in standard Java:
String[] x = Pattern.compile(";").split(CookieManager.getInstance().getCookie());
Now you have an Array of name - value pairs, which you can further parse and then store.