I have a service and send Datadog events from it using com.github.arnabk.java-dogstatsd-client. In order to send json string I use JsonObject where I put all properties which I need then convert it to string using toString() method on JsonObject and send string as a message body. Everything works perfect unless I have a character in a string which is not from english alphabet. Example: µ. In this case instead of having correct json {"Smth":"µ"} in Datadog I'm getting incorrect string without closing curly brace {"Smth":"µ". Has anybody experienced the same and knows how to deal with this?
Related
My Flutter app calls a REST API method /user/search/<search string> and I am forming the URL endpoint using encodeQueryComponent like this:
String endpoint = "/user/search/"+Uri.encodeQueryComponent(searchString);
The back-end implemented in Java tries to retrieve the search string like this:
String value = URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
However, when the search string contains the + sign, the raw encode string in the back-end contains %2B and the decoded String contains space. As a temporary hack, I am currently doing value = value.replace("%2B", "+"); instead of decode. But this is obviously not the right approach because the search string may contain characters from any language or special characters.
Can someone tell me what is the right way to get the original string sent by the user in Java?
I need to send a request to a RESTful Service which contains the '≈' special character.
The Request Body should look like this:
{
"searchText":"Pipe ≈ 1,25d"
}
The '≈' symbol is not included in UTF-8 encoding.
In case you know how to pack this character in a Java String, please let me know.
Being able to type it depends on your keyboard and setup. You can, however, copy/paste strings into Java source.
String s = "Pipe ≈ 1,25d";
I know this question was asked several times before on this forum. But none of it addresses my problem.
I am using Jmeter to post json string with content-type "application/json"
My Spring mvc app is happy consuming the same and converting it to Java Object.
However, when i send a string with a backslash everything seems to fall apart.
Here is my Json Input string
{"signup":{"firstName":"joe", "lastName":"beedu","email":"jb1407874596956#hotmail.com",
"password":"O;\l-wslD6RQ5#M)","confirmPassword":"O;\l-wslD6RQ5#M)",
"picImage":[81,109,81,109,109,81,109,109,89]}}
Note: password fields have a back slash. More on this later.
Here is my java class for the same.
#JsonRootName(value = "signup")
#XmlRootElement(name = "signup")
public class Signup {
private String firstName;
private String lastName;
private String email;
private String password;
private String confirmPassword;
private byte[] picImage;
}
Removed getters/setters and also validation annotations.
Issue number 1: When i manually escape the password field values with an extra slash like
"O;\l-wslD6RQ5#M)" My Spring mvc web app is happy, but doesn't automatically unescape the extra back slash for me. I was expecting my spring converter MappingJackson2HttpMessageConverter to read the json payload and do the magic unescape thing for me, but it doesn't. It will be very ugly if i manually unescape every field in json json objects. Any help here to address this?
Issue number 2: On the client side, i tried several different ways to see if any library automatically escapes the strings in Json object. Given a raw json string like above, is there a way to escape the string that goes into json object? I tried, StringEscapeUtils from apache escapeJson, it didn't work. I tried json-lib too. None of them were able to handle
string "O;\l-wslD6RQ5#M)". I had to manually add extra back slash before i add the json string to Http Body.
I would like to use a client side library that escapes json string fields and also on the server side to unescape. I deeply appreciate you guys for reading a lengthy question. I wasted a day on this one. I hope you guys will help me out with an answer or at least a direction.
Thanks
Sri
In my Android application I get JSON response string from a PHP url. from the response I get some hotel names with apostrophe, I get ' character instead of apostrophe. How can I parse the hotel with special characters in android? I can see the apostrophe in the browser but could not see in android logcat.
I have tried jresponse = URLEncoder.encode(jresponse,"UTF-8"); but I could not get apostrophe for hotel name.
This is the one of the hotel name in the response.
I see the following in browser.
{"id":12747,
"name":"Oscar's",
....
}
But in the logcat:
id 12747
name Oscar's
Use the decoder instead of encoder. URLDecoder.decode(jresponse,"UTF-8")
Use ISO-8859-2 when you create the URLEncodedEntity that you send off. You can set this as a parameter in the constructor.
Without a specified charset, you are probably sending the data in UTF-8/UTF-16 (most common) which the server is interpreting in a different way.
EDIT: It looks like ISO-8859-2 doesn't support ñ. You may have to change something server-side. http://en.wikipedia.org/wiki/ISO/IEC_8859-2
You can try Html class. eg :-
jresponse = Html.fromHtml(jresponse);
My application has a Java servlet that reads a JSONObject out of the request and constructs some Java objects that are used elsewhere. I'm running into a problem because there are strings in the JSON that are encoded in ISO-8859-1. When I extract them into Java strings, the encoding appears to get interpreted as UTF-16. I need to be able to get the correctly encoded string back at some point to put into another JSON object.
I've tried mucking around with ByteBuffers and CharBuffers, but then I don't get any characters at all. I can't change the encoding, as I have to play nicely with other applications that use ISO-8859-1.
Any tips would be greatly appreciated.
It's a legacy application using Struts 1.3.8. I'm using net.sf.json 2.2.4 for JSONObject and JSONArray.
A snippet of the parsing code is:
final JSONObject a = (JSONObject) i;
final JSONObject attr = a.getJSONObject("attribute");
final String category = attr.getString("category");
final String value = attr.getString("value");
I then create POJOs using that information, that are retrieved by another action class to create JSON to pass to the client for display, or to pass to other applications.
So to clarify, if the JSON contains the string "Juan Guzmán", the Java String contains something like Juan Guzm?_An (I don't have the exact one in front of me). I'm not sure how to get the correct diacritical back. I believe that if I can get a Java String that contains the correct representation, that Mezzie's solution, below, will allow me to create the string with the correct encoding to put back into the JSON to serve back.
I had the same issue and I am using the same technology as you are. In our case, it was UTF 8. so just change that to UTF-16
public static String UTF8toISO( String str )
{
try
{
return new String( str.getBytes( "ISO-8859-1" ), "UTF-8" );
}
catch ( UnsupportedEncodingException e )
{
e.printStackTrace();
}
return str;
}