gpt4 book ai didi

java - JAVA中的JSON数组错误,并在URL中添加了反斜杠

转载 作者:行者123 更新时间:2023-12-01 20:27:08 24 4
gpt4 key购买 nike

我有以下 JSON 数组,我使用 POST 方法将其发送到 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
}

在 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循环中,每个元素的值是这样的:

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

我不知道问题出在哪里!

最佳答案

发生这种情况的原因:

有效的 JSON 字符串将始终在“/”之前包含“\”以进行表示。

看图片:

JSON String Representation

JAVA 在这里所做的是通过在“/”之前添加“\”将 JSON 转换为有效的 JSON。

解决方案是在JAVA中处理字符串,并通过从字符串中删除'\'字符将其转换为原始格式

这个可以引用答案: remove-all-occurrences-of-char-from-string

关于java - JAVA中的JSON数组错误,并在URL中添加了反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43735022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com