0

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!

Majico
  • 3,810
  • 2
  • 24
  • 36
  • For the added backslash. I have seen this with the standard JSON API in Android. It is said they used this logic to be able to use those value in a GET request. You can use a different API if this is really a problem or use the same API to decode it. For the array, since you `json.getJSONArray("images");` but I don't see any `images` key in your JSON, this is complicated to answer. You might need to post a [mcve] to find an asnwer – AxelH May 02 '17 at 10:09
  • I have updated the JSON – Majico May 02 '17 at 10:20
  • 2
    You have an array of array, so the array you want is actually in imagesToProcess [0]. But this should output you a `["https:\/\/...` not `[["https:\/\/...` – AxelH May 02 '17 at 10:23
  • @AxelH : thank you , I didn't pay attention which it is an array of array, so by getting the [0] i could make everything work fine. even the problem of backslash doesn't exist any more because i pass a correct URL and by using .getString(i) i could have the correct URL in my string variable.Thank you again – Majico May 02 '17 at 11:49
  • Indeed, since this is the API that adds those `"\"`, during the reading of a String with the same API those escaped character are removed. And as @Anil point it in his answer, this format is a specification of the JSONotation, you can find it in the [RFC 7159](https://tools.ietf.org/html/rfc7159) – AxelH May 02 '17 at 12:01

2 Answers2

2

The Reason why This is Happening:

A Valid JSON String will always Contain "\" before "/" for Representation.

See Image:

JSON String Representation

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

Community
  • 1
  • 1
Anil Uttani
  • 111
  • 5
1

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 :

Community
  • 1
  • 1
AxelH
  • 14,325
  • 2
  • 25
  • 55