JSON object adding extra back slash - java

What i am doing currently creating json object and passing to volley for service call, its placing back slash in jsonObject in key value result is getting 404. following is my code and creating jsonObject.
JSONObject params = new JSONObject();
params.put("user_name", "syedshah11#gmail.com");
params.put("password", "6e7a0497daffa4554cc28973bc129632");
params.put("key", "ly9jCDu03/1:3M1");
And as i debuged the josnObject adding extra \ (forward slash in my key json object thats why service break through as 404) following is json getting through debug.
{
"password": "6e7a0497daffa4554cc28973bc129632",
"user_name": "syedshah11#gmail.com",
"key": "ly9jCDu03\/1:3M1"
}
So how to remove this \ extra forward slash as instead of this ly9jCDu03/1:3M1 its making ly9jCDu03\/1:3M1 any help could be appreciated.
Thanks in Advance

Java is encoding your JSON string for sending to end point. Slash (/) is not an issue, if request not build with proper manners and there is any problem at Java end then you receive 500 Internal Server Error or volley error.
Be confident on your code and check service.

when json convert key value to string "ly9jCDu03/1:3M1" it adds extra '\' to represent the '/' as '/' is a special character and to represent that u need '\'
it is just like when u want to print '/n' in c++ u need to add extra '\'

Related

How can I prevent the automatic decoding of RequestParam Strings in a Spring controller

I have an HTTP GET request controller endpoint where I take in a fileName as a query param and pass that on to another service. For this request the param the filename could include any sort of special characters and I would like to keep these values encoded when passing them on. 2 Characters that have been causing issues are spaces (%20) and +(%2B).
How can I keep these characters encoded in the request params.
So far I have tried using the #RequestParam annotation as well as retrieving the params via HttpServletRequest.getParameterValues(String) but both return the decoded values as spaces.
Any help is appreciated thanks!
Yes, these are automatically decoded by the servlet API. You should be able to re-encode them -
encodedValue = URLEncoder.encode(value, StandardCharsets.UTF_8);
I found out that I could get the actual value passed in by using the HttpServletRequest.getQueryString() method. Parsing this query string I was able to get the un-decoded version of the fileName being passed in. I hope this helps someone in the future.

Wrong JSON array in JAVA, and added back slash in URL

I have the following JSON array which I use POST method to send it to BackEnd:
{"images":
[["https:\/\/storage.googleapis.com\/shelf-prove\/test1.jpg",
"https:\/\/storage.googleapis.com\/shelf-prove\/test2.jpg",
"https:\/\/storage.googleapis.com\/shelf-prove\/test3.jpg",
"https:\/\/storage.googleapis.com\/shelf-prove\/test5.jpg"]],
"skus":["
{\"id\":5179846254657536,\"coordinates\":\"137,447,692,438,690,610,140,617\",\"sku\":\"Biscotti\"}",
"{\"id\":5656058538229760,\"coordinates\":\"0,116,303,104,310,264,2,282\",\"sku\":\"Riso\"}",
"{\"id\":5765606242516992,\"coordinates\":\"140,614,675,610,673,751,145,755\",\"sku\":\"Succo\"}"],
"percentage":"33",
"model":5682617542246400,
"shelf":5660980839186432
}
in Java I try to get it as JSON array with the following code :
imagesToProcess = json.getJSONArray("images");
for(int i = 0; i < imagesToProcess.length(); i++){
String src="";
src = imagesToProcess.getString(i); }
the problem is that in java i see the value of the array as following:
[["https:\/\/storage.googleapis.com\/shelf-prove\/test1.jpg","https:\/\/storage.googleapis.com\/shelf-prove\/test2.jpg","https:\/\/storage.googleapis.com\/shelf-prove\/test3.jpg","https:\/\/storage.googleapis.com\/shelf-prove\/test5.jpg"]]
and in for loop, the value of each lement is like this:
[["https:\/\/storage.googleapis.com\/shelf-prove\/test1.jpg","https:\/\/storage.googleapis.com\/shelf-prove\/test2.jpg","https:\/\/storage.googleapis.com\/shelf-prove\/test3.jpg","https:\/\/storage.googleapis.com\/shelf-prove\/test5.jpg"]]
I don't know what's the problem!
The Reason why This is Happening:
A Valid JSON String will always Contain "\" before "/" for Representation.
See Image:
what JAVA Does here is Converts the JSON to a Valid JSON by adding "\" before "/" .
The Solution is to Process the String in JAVA and convert it to Original Format by removing '\' character occurences from String
For that You can Refer to Answer:
remove-all-occurrences-of-char-from-string
To answer your probably in order, you are trying to get the value for an array of array, this looks like an mistake of encoding since the first array is of 1 cells. So get the array in that cell then iterate, you have a correct code for that.
Then about the escaped character, you can see in the RFC 7159 - The JavaScript Object Notation (JSON) Data Interchange Format
From 7. Strings :
Any character may be escaped.
But there is no specification about which one, you can see in an example :
"Thumbnail": {
"Url": "http://www.example.com/image/481989943",
"Height": 125,
"Width": 100
},
The URL has no escape "/" so this is API specific.
There is know question about that problem on SO like :
JSON: why are forward slashes escaped?
Why is the slash an escapable character in JSON? [duplicate]

How to read json with backslashes with Jackson

I'm calling 3rd party API and receiving as a response json:
{\"name\":\"name \\"A\\" and other\",\"id\":1}
If I try to map it like that I'm getting sure that:
Unexpected character ('\' (code 92)): was expecting double-quote to start field name
How could I map it with jackson? Should I remove backslashes with regex? Like every \" -> " and \\" -> \"
That is not well formed. What are you planning to do looks fine
File a bug against whoever is producing this broken output; it is not valid JSON.
{\"name\":\"name A and other\",\"id\":1}, this is the valid form of JSON or
{"name":"name A and other","id":1} it is also valid.
If this is not possible to do it, ask your vendor to validate the JSON structure

How to convert Json string with escape characters to json in Android

I am getting the following json as a reponse of a rest call. I am unable to parse it. Replacing "\" with "" doesn't work as the string contains many escape characters like "\n".
"[{\"message_id\":50870,\"message\":\"4d074d54-6e08-a140-fb7a-ee1300b01fbf.png\"},
{\"message_id\":50823,\"message\":\"1\\n2\\n3\\n4\\n5\\n6\"},{\"message_id\":50341,\"message\":\"I am getting a \\\"Server Error\\\" }]"
I have tried JsonTokener, UrlDecoder, but nothing seems to work.
I have also tried using
JsonString.replace ("\\"", "\"");
This works but is there a better way for conversion
JSONSerialiser serialiser = new JSONSerialiser();
String jsonOutput= serialiser.include("id","message").exclude("*").serialize(javaobject);
JSONObject jObject = JSONFactoryUtil.createJSONObject(jsonOutput);

Write correctly an URL in JSON with Java

I need to write an attribute on a JSON document, and this attribute is an URL
This is my code:
String url = "http://localhost:1028/accumulate";
JSONObject cabecera = new JSONObject();
cabecera.put("reference", url);
But when I create the JSON,this attibute is writted in this way:
"reference":"http:\/\/localhost:1028\/accumulate",
So, the service that receives the JSON String, it's sending me an error:
<subscribeContextResponse>
<subscribeError>
<errorCode>
<code>400</code>
<reasonPhrase>Bad Request</reasonPhrase>
<details>JSON Parse Error: <unspecified file>(1): invalid escape sequence</details>
</errorCode>
</subscribeError>
</subscribeContextResponse>
What is the correct way to write the URL??
Thanks in advance
But when I create the JSON,this attibute is writted in this way:
"reference":"http:\/\/localhost:1028\/accumulate",
That's fine, the backslashes are harmless, whatever you're using to serialize to JSON is just being a bit hyper with its escapes. The string represented by the above contains no backslashes at all, just slashes. \/ inside a JSON string is exactly the same as /, as we can see from the highlighted rule from http://json.org:
("solidus" is a fancy term for slash)

Categories