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

标签 java json urlencode data-conversion objectmapper

我希望从 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/

相关文章:

javascript - php 未输出 JSON 数据

json - 无法获得YouTube视频的标题

python - 如何使用 Python 编码和解码字符串以用于 URL?

javascript - 如何在同时支持window和linux的javascript数组下转义 ':'、 ","和 '()'?

javascript - 打开 javascript 窗口的 url 编码

java - 如何比较对象数组中的对象?

java - 使用eclipse调用不同子项目中的类

php - 正则表达式在冒号后的每个单词周围加上引号

Java ObjectInputStream(服务器\客户端)

Java 和 MYSQL 语法问题