2017-05-02 20 views
0

我有以下的JSON数组我使用POST方法将其发送到后端:错误的JSON数组在Java中,并添加回URL削减

{"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 
} 

在Java中我试图把它作为JSON阵列以下代码:

imagesToProcess = json.getJSONArray("images"); 
for(int i = 0; i < imagesToProcess.length(); i++){ 
     String src=""; 
     src = imagesToProcess.getString(i); } 

的问题是,在Java我看到数组的值如下:

[["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"]] 

和for循环,每个lement的值是这样的:

[["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"]] 

我不知道是什么问题!

+0

对于添加反斜杠。我在Android中看到了这个标准的JSON API。据说他们使用这种逻辑能够在GET请求中使用这些值。如果这确实是个问题,或者使用相同的API来解码,则可以使用其他API。对于数组,因为你的json.getJSONArray(“images”);'但是在你的JSON中没有看到任何'images'键,这个答案很复杂。您可能需要发布[mcve]以查找未知数 – AxelH

+0

我已更新JSON – Majico

+2

您有一个数组数组,因此您想要的数组实际上位于imagesToProcess [0]中。但是这应该会输出一个'[“https:\/\/...'不是'[[”https:\/\/...' – AxelH

回答

1

要将可能回答为了获得数组数组的值,这看起来像编码错误,因为第一个数组是1个单元格。所以得到那个单元格中的数组然后迭代,你有一个正确的代码。

然后对转义字符,你可以在RFC 7159 - The JavaScript Object Notation (JSON) Data Interchange Format

看到从7.字符串

任何字符可以被转义。

但没有说明哪一个,你可以在一个例子中看到:

"Thumbnail": { 
      "Url": "http://www.example.com/image/481989943", 
      "Height": 125, 
      "Width": 100 
}, 

的URL没有逃逸“/”,所以这是特定的API。

有了解的如此喜欢这个问题的问题:

+0

中找到它。感谢您提供您的评论作为回答:) – Majico

+0

@Majico欢迎您,请注意,我已更新转义部分。 – AxelH

2

之所以发生这种情况:

一个有效的JSON字符串将总是先“/”为代表含有“\”。

见图片:

JSON String Representation

什么JAVA请问这里是 “/” 前加上 “\” 的JSON转换为有效的JSON。

解决方案是处理Java中的字符串,并将其通过从字符串

转换为原始格式“\”字符OCCURENCES对于您可以参考答案: remove-all-occurrences-of-char-from-string