JsonParser decoding the unicode - java

I am converting a JSON string into a JsonObject, by using JsonParser and JsonElement.
But my JSON string contains few Unicode escape sequence, and after parsing into JsonElement, Unicode escape sequence automatically converting into the actual element.
But I don't want to convert the Unicode escape sequence to the actual element after parsing.
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(jsonString);
JsonObject jsonObject = jsonElement.getAsJsonObject();
jsonString is as below :
{
"config": {
"title": "Check the String \u0026 verify"
}
}
After parsing into JsonElement result is coming as below:
{"config":{"title":"Check the String & verify"}}
But I don't want to decode \u0026 into & after parsing.
Please suggest to me how I will resolve.
Note: I am using google gson to parse and create JsonElement, JsonObject

If you parse JSON with escape characters and you don't want them to be converted to the actual symbol, just escape the backslash.
"Check the String \u0026 verify" would then be "Check the String \\u0026 verify"

I know that it's a bad solution, but you can replace the \u0026 with \\u0026 before parsing it.
JsonElement jsonElement = JsonParser.parseString(jsonString.replaceAll("\\\\u([0-9a-fA-F]{4})", "\\\\\\\\u$1"));

Related

Throwing error while parsing- org.bson.json.JsonParseException: Invalid escape sequence in JSON string '\Q'

I need to parse json string to Document to store in mongodb.
When i am going to parse json string using Document.parse(jsonStr), if jsonstr contains "\" then it will throw org.bson.json.JsonParseException: Invalid escape sequence in JSON string '\Q'.
Sample Code
String json="{'a':'as\\Qd'}"; //Json String with "\"
Document doc = Document.parse(json);//Going to parse jsonstring which has "\"
System.out.println(doc.toJson());

Text to Json in Java (list in text)

I have the following text which is returned by an API that I call:
{"identified_faces_names": "[\"omar\", \"elhoussinep\"]"}
when I pass this text to JSONParse to parse it, it give me the following exception:
Exception in thread "main" Unexpected character (o) at position 0.
Code used to parse the text to json:
String s = new String("{\"identified_faces_names\": \" [\"omar\",\"elhoussinep\"]\"}");
JSONParser parser = new JSONParser();
Object obj = parser.parse(s);
Did you definitely mean to enclose ["omar", "elhoussinep"] in quotes? Is that definitely intended to be a String value containing string quotes? Or is it intended to be an array of strings?
If identified_faces_names is intended to be an array of strings then the valid JSON is:
{
"identified_faces_names": [
"omar",
"elhoussinep"
]
}
This is parseable, without error, like so:
String s = new String("{\"identified_faces_names\": [\"omar\",\"elhoussinep\"]}");
JSONParser parser = new JSONParser();
Object obj = parser.parse(s);
If identified_faces_names is intended to be a String containing quotes you must escape the quotes inside the string. The valid JSON is:
{
"identified_faces_names": "[\\\"omar\\\",\\\"elhoussinep\\\"]"
}
This is parseable, without error, like so:
String s = new String("{\"identified_faces_names\": \" [\\\"omar\\\",\\\"elhoussinep\\\"]\"}");
JSONParser parser = new JSONParser();
Object obj = parser.parse(s);
So, in summary, I'd suggest revisiting the JSON to determine whether it is an array of strings or a string which contains quotes, if the latter then you have to escape those quotes.
FWIW, you can use JSONLint to check whether the JSON is valid. Using this you can see that your original JSON ({"identified_faces_names": "["omar","elhoussinep"]"}) was not valid and that the first invalid character is the "o" in "omar" and that's deemed invalid because it follows "[" which is deemed to be a complete String.
Use this String {"identified_faces_names": [\"omar\", \"elhoussinep\"]}. It is correct and will parse.

Fetch String with special character from json using JSONObject

I have a JSON object with special character in it.
The format is as follows:
"field1": "result1", "field2": "\uabc\udef\ughi"
I get the string for each of the keys as
JSONObject jsonObj = new JSONObject();
String s1 = jsonObj.getString("field1");
String s2 = jsonObj.getString("field2");
When I print s2, I get weird characters as output. I know, "\u" is doing all the weirdness and I do not want that. I just want to get the string for field2 as it is. Sadly, I cannot modify the JSON object at source.
Any solution to this scenario.

Get string from JSON like PHP in Java

I was wondering if there is a way to get a string from JSON in Java like there is in PHP:
<?php
$json = #file_get_contents('example');
$decoded_json = json_decode($json);
echo $decoded_json->{"something"} ;
?>
I have currently tried:
String input = "[{"minecraft.net":"green"},{"session.minecraft.net":"green"},{"account.mojang.com":"green"},{"auth.mojang.com":"green"},{"skins.minecraft.net":"green"},{"authserver.mojang.com":"green"},{"sessionserver.mojang.com":"green"},{"api.mojang.com":"green"},{"textures.minecraft.net":"green"}]";
String wanted = "minecraft.net";
JSONObject json = new JSONObject(input);
String out = json.getString(wanted);
System.out.println(out);
However, it gives me this error:
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
Thanks!
It's because your String is not a valid JSON object but a valid JSON array which contains objects, try to use JSONArray instead then you can query around this array for your desired object
You forgot to add \" when a double quote is inside another double quote

Parsing String value into JsonElement gives MalformedJsonException

I am trying to create a JsonElement using the following code:
String updateUrl = myurl + "/new_url";
JsonParser parser = new JsonParser();
JsonElement updateUrlJsonElement = parser.parse(updateUrl);
Gives me
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected EOF at line 1 column 6
at com.google.gson.JsonParser.parse(JsonParser.java:65)
at com.google.gson.JsonParser.parse(JsonParser.java:45)
Any ideas, how can i create a JSONElement with just a String value.
According to the JSON format, a JSON string is enclosed in double quotes. You'll have to enclose your String value in double quotes.
String updateUrl = "\"" + myurl + "/new_url" + "\"";
Note that you can also just create a JsonPrimitive, a subtype of JsonElement, with the given String.
new JsonPrimitive(updateUrl); // without the quotes

Categories

Resources