I have a JSON-formatted string with characters like \u0025
that I need to convert into their actual character representations.
Is there a direct method in Java to perform this conversion?
Perhaps something like myString.convertToUTF-8();
?
I am utilizing the Graph API and receiving responses in JSON format. I intend to use a JSON parser or JavaScript parser for extracting the values.
When trying to assign the response to a variable using the Google Chrome console, I encountered an error stating
Uncaught SyntaxError: Unexpected identifier(…)
.
In JavaScript, I wanted to use JSON.parse(jsonString)
, but unfortunately, I couldn't successfully assign a value to jsonString
.
To convert these characters in Java, I have attempted the following lines of code which did not alter the string:
String responseFromFB = http.sendRequest(url);
byte[] b = responseFromFB.getBytes();
String str= new String(b, "UTF-8");
Below is the sendRequest() function that I'm currently using:
private String httpReq(String URL, String QueryString) throws Exception
{
String url = URL;
String urlParameters = QueryString;
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}