gpt4 book ai didi

java - 将 url 编码数据转换为 json

转载 作者:行者123 更新时间:2023-12-02 09:21:42 25 4
gpt4 key购买 nike

我希望从 webhook 获取 JSON 数据。

我得到下面这种形式的数据,内容/类型是 application/x-www-form-urlencoded 而不是 application/json

results%5B6%5D%5Bid%5D=7&results%5B18%5D%5Bid%5D=19&results%5B0%5D%5Bname%5D=data+autre&results%5B1%5D%5Bname%5D=data2+autre&assessments%5B0%5D%5Bstatus%5D=finish&results%5B10%5D%5Bscore%5D=6&results%5B7%5D%5Bname%5D=data3&results%5B6%5D%5Bname%5D=Accept&results%5B8%5D%5Bname%5D=data4&results%5B2%5D%5Bname%5D=autres&results%5B3%5D%5Bname%5D=data6&results%5B4%5D%5Bname%5D=autre&results%5B5%5D%5Bname%5D=autres3&results%5B9%5D%5Bname%5D=data8&results%5B17%5D%5Bid%5D=18&reports%5B4%5D%5Bid%5D=8&reports%5B4%5D%5Bis_available%5D=0&results%5B7%5D%5Bscore%5D=7&results%5B17%5D%5Bscore%5D=4&reports%5B1%5D%5Bis_available%5D=1&assessments%5B2%5D%5Blink%5D=https%3A%2F%2Ftest%3D123&lastname=aaa&results%5B3%5D%5Bscore%5D=10&reports%5B3%5D%5Bid%5D=15&results%5B16%5D%5Bid%5D=17&register_link=&results%5B7%5D%5Bid%5D=8&results%5B19%5D%5Bid%5D=20&results%5B13%5D%5Bscore%5D=5&assessments%5B1%5D%5Bstatus%5D=todo&results%5B4%5D%5Bid%5D=5&status=accepted&results%5B9%5D%5Bid%5D=10&results%5B15%5D%5Bid%5D=16&results%5B3%5D%5Bid%5D=4&reports%5B4%5D%5Bname%5D=data9&reports%5B3%5D%5Bname%5D=data10&results%5B18%5D%5Bscore%5D=1&email=test@test.com&results%5B9%5D%5Bscore%5D=6&synthesis=

如何将其转换为 json ?

谢谢

最佳答案

如果您想在 java 中转换它,也许您可​​以尝试以下代码:

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class URLEncodeDecode {
public static void main(String[] args) {
String url2 = "results%5B6%5D%5Bid%5D=7&results%5B18%5D%5Bid%5D=19";
String decodeURL = decode(url2);
System.out.println("Decoded URL: " + decodeURL);

System.out.println(Stream.of(decodeURL.split("&")).map(elem -> new String(elem)).collect(Collectors.toList()));

List<String> uriToList = Stream.of(decodeURL.split("&")).map(elem -> new String(elem))
.collect(Collectors.toList());

Map<String, String> uriToListToMap = new HashMap<>();

for (String individualElement : uriToList) {
uriToListToMap.put(individualElement.split("=")[0], individualElement.split("=")[1]);
}

// Use this builder to construct a Gson instance when you need to set
// configuration options other than the default.
GsonBuilder gsonMapBuilder = new GsonBuilder();

Gson gsonObject = gsonMapBuilder.create();

String uriToJSON = gsonObject.toJson(uriToListToMap);
System.out.println(uriToJSON);

}

public static String decode(String url) {
try {
String prevURL = "";
String decodeURL = url;
while (!prevURL.equals(decodeURL)) {
prevURL = decodeURL;
decodeURL = URLDecoder.decode(decodeURL, "UTF-8");
}
return decodeURL;
} catch (UnsupportedEncodingException e) {
return "Issue while decoding" + e.getMessage();
}
}

}

关于java - 将 url 编码数据转换为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58656205/

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