I'm building a JSON string to send to my web service. Since one of the pieces is user-inputted, there is the possibility for double quotes. I'm trying to resolve the issue by escaping it.
String strValue = "height of 6\"";
JSONObject json = new JSONObject();
json.put("key", strValue.replaceAll("\"","\\\""));
The problem here is when I do json.toString(), I get 3 slashes.
Ex:
{"key","height of 6\\\""}
If I don't try to do any replacing, json.toString() gives me broken json.
Ex:
{"key", "height of 6""}
How can I do this correctly?
Note: When my website saves this value and displays it, it displays height of 6\"
UPDATE:
It appears the culprit is json.toString()
When I call the replaceAll method it -- correctly -- only escapes the double quote. It appears json.toString() escapes slashes. To fix the issue, I must do json.toString().replace("\\\\", ""). This begs the question: Why on Earth does JSONObject escape slashes and not double quotes?????
It appears the culprit is json.toString()
When I call the replaceAll method it -- correctly -- only escapes the double quote. It appears json.toString() escapes slashes. To fix the issue, I must do json.toString().replace("\\\\", "").
This begs the question: Why on Earth does JSONObject escape slashes and not double quotes?????
Related
I want to convert a UTF-8 string to escape \uXXX format in value of JSON Object.
I used both JSON Object and Gson, but did not work for me in this case:
JSONObject js = new JSONObject();
js.put("lastReason","nguyễn");
System.out.println(js.toString());
and
Gson gson = new Gson();
String new_js = gson.toJson(js.toString());
System.out.println(new_js);
Output: {"test":"nguyễn"}
But i am expect that my result is:
Expected Output: {"test":"nguy\u1EC5n"}
Any solutions for this case, please help me to resolve it.
You can use apache commons-text library to change a string to use Unicode escape sequences. Use org.apache.commons.text.StringEscapeUtils to translate the text before adding it to JSONObject.
StringEscapeUtils.escapeJava("nguyễn")
will produce
nguy\u1EC5n
One possible problem with using StringEscapeUtils might be that it will escape control characters as well. If there is a tab character at the end of the string it will be translated to \t. I.e.:
StringEscapeUtils.escapeJava("nguyễn\t")
will produce an incorrect string:
nguy\u1EC5n\t
You can use org.apache.commons.text.translate.UnicodeEscaper to get around this but it will translate every character in the string to a Unicode escape sequence. I.e.:
UnicodeEscaper ue = new UnicodeEscaper();
ue.translate(rawString);
will produce
\u006E\u0067\u0075\u0079\u1EC5\u006E
or
\u006E\u0067\u0075\u0079\u1EC5\u006E\u0009
Whether it is a problem or not is up to you to decide.
I am having user defined object Customer which has multiple attributes ,in one of the attributes we can have single , double quotes and backslash as well. While converting the object to string Gson library is adding backslash in it.
I am using below code to achive this but it is not working.
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
JsonElement jsonString = gson.toJsonTree(triggerModel);
Output is
{
"customerId": "1234",
"customerName": "Loren",
"customerAddress": [
{
"postalcode": "67676",
"lane": "\"LA16767",
"houseNumber": "2025",
"society": "null"
}
]
}
In lane attribute the original value was "LA16767 but it is adding one backslash character. How to write this in such a way string with single ,double quotes and backslash are handled using same line of code.
Output provided by Gson is correct because "lane": ""LA16767" would not have been a valid json.
From json docs
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.
You had mentioned :
How to write this in such a way string with single ,double quotes and backslash are handled using same line of code.
You need not do anything special to handle single ,double quotes and backslash characters. Gson will automatically escape them for you.
Any app (server, UI, etc,) who is consuming your json, will correctly parse "\"LA16767" as "LA16767 going by the json conventions.
I am trying to parse an HTML document. In the document, there is the
span-data-personalization = '{"one":["two"]}' which converts to
span-data-personalization = "{"one":["two"]}" while parsing. The double quotes convert to " and single quotes to double quote. I have also used doc.outputSettings().prettyPrint(false); with no success. Also, made the changes suggested in jsoup - stop jsoup from making quotes into & It still did not work. And, I have also tried updating the Jsoup version.nothing seems to work. Does anybody have any suggestions?
Thank you.
The JSoup Parser class has a built in unescapeEntities method. From the JSoup documentation:
public static String unescapeEntities(String string,
boolean inAttribute)
Utility method to unescape HTML entities from a string
Parameters:
string - HTML escaped string
inAttribute - if the string is to be escaped in strict mode (as attributes are)
Returns:
an unescaped string
I am parsing below string value into OData query through java code.
objects.put("EndDate", "\/Date(1441756800)\/";
How can i parse the /Date(1441756800)/ into a string in java.
I have tried with below :
objects.put("EndDate", ""\\""//"Date(1441756800)""\\""//"";
throws error:(
I never used OData so I may not understand your question correctly, but if you are asking how to write \/Date(1441756800)\/ as String then you need to escape \ as it is String special character (used for instance when escaping or when creating other special characters like line separators \n).
So try with "\\/Date(1441756800)\\/"
Try this - objects.put("EndDate", "'Date(1441756800)'";
I am doing the following:
String url = String.format(WEBSERVICE_WITH_CITYSTATE, cityName, stateName);
String urlUtf8 = new String(url.getBytes(), "UTF8");
Log.d(TAG, "URL: [" + urlUtf8 + "]");
Reader reader = WebService.queryApi(url);
The output that I am looking for is essentially to get the city name with blanks (e.g., "Overland Park") to be formatted as Overland%20Park.
Is it this the best way?
Assuming you are actually wanting to encode your string for use in a URL (ie, "Overland Park" can also be formatted as "Overland+Park") you want URLEncoder.encode(url, "UTF-8"). Other unsafe characters will be converted to the %xx format you are asking for.
The simple answer is to use URLEncoder.encode(...) as stated by #Recurse. However, if part or all of the URL has already been encoded, then this can lead to double encoding. For example:
http://foo.com/pages/Hello%20There
or
http://foo.com/query?keyword=what%3f
Another concern with URLEncoder.encode(...) is that it doesn't understand that certain characters should be escaped in some contexts and not others. So for example, a '?' in a query parameter should be escaped, but the '?' that marks the start of the "query part" should not be escaped.
I think that safer way to add missing escapes would be the following:
String safeURI = new URI(url).toASCIIString();
However, I haven't tested this ...