【问题标题】:Convert url encoded data to json将 url 编码的数据转换为 json
【发布时间】:2019-11-01 08:31:27
【问题描述】:

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

谢谢

【问题讨论】:

  • 解码的文本不是 json(它实际上以“results[6][id]=7&results[18][id]=19&results[0][name]=data+autre”开头) -所以它不仅仅是解码,而是你应该从 webhook 所有者那里找到你如何请求 json (例如,这可能会有所帮助:stackoverflow.com/questions/43209924/…
  • 天真的 json 翻译 {"results[6][id]" : "7", "results[18][id]" : "19", "results[0][name]" : "data autre"} 似乎也不是一个可用的数据结构。
  • 是的@racraman 我知道这不是 json 这就是我发布此内容的原因,API 所有者无法将内容类型从 application/x-www-form-urlencoded 更改为 application/json,如果我添加 Accept=application/json 我不会再有数据了
  • 是的@JoopEggen 它不是..

标签: java json urlencode data-conversion objectmapper


【解决方案1】:

如果您想在 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();
        }
    }

}

【讨论】:

  • 我得到的 json 仍然包含 [number][id] ..... 它仍然不可用.. 顺便说一句,我得到了和你一样的结果,但只使用了几行。
  • 好的,如果字符串是 "results%5B6%5D%5Bid%5D=7&results%5B18%5D%5Bid%5D=19" ,那么预期的 JSON 输出是多少?请分享一下预期的输出?
  • 预期的输出是:{"results": [ {"id": ,"name": ,"score":}, ...], "reports" : [ {"id" : ,"name": ,"is_available": ,"link": }, ... ] , "email": } 类似这样的
猜你喜欢
  • 1970-01-01
  • 2017-10-25
  • 2019-05-03
  • 2013-09-09
  • 2017-10-13
  • 2016-05-09
  • 1970-01-01
  • 2021-05-05
  • 2015-04-27
相关资源
最近更新 更多