JS decodeURIComponent 与 JAVA 解码

前端用 decodeURIComponent 编码字符串后,会生成类似

%7B%22mailNo%22%3A%223322826833883%22%2C%22status%22%3A%220%22%2C%22errCode%22%3A%221%22%2C%22message%22%3A%22%5Cu67e5%5Cu8be2%5Cu4e0d%5Cu5230%5Cu4fe1%5Cu606f%5Cuff0c%5Cu8bf7%5Cu6838%5Cu5bf9%5Cu5355%5Cu53f7%5Cu548c%5Cu5feb%5Cu9012%5Cu516c%5Cu53f8%5Cu662f%5Cu5426%5Cu6b63%5Cu786e%22%2C%22expSpellName%22%3A%22shentong%22%2C%22expTextName%22%3A%22%5Cu7533%5Cu901a%5Cu5feb%5Cu9012%22%2C%22tel%22%3A%2295543%5C%2F4008895543%22%2C%22data%22%3A%5B%5D%2C%22update%22%3A%221486536329%22%2C%22cache%22%3A7%2C%22ord%22%3A%22ASC%22%7D

这样的形式。
但是这样的形式用Java不好处理,网上大部分都说用

String result = java.net.URLDecoder.decode(url, "UTF-8");

但是效果不好,内容里如果包含双引号的话,双引号里的内容没有被解码出来。

最后在StackOverFlow上找到了,解决方案很简单

String result = new java.net.URI(url).getPath();

对,你没有看错,getPath()可以完美解码。

{
    "mailNo": "3322826833883",
    "status": "0",
    "errCode": "1",
    "message": "查询不到信息,请核对单号和快递公司是否正确",
    "expSpellName": "shentong",
    "expTextName": "申通快递",
    "tel": "95543/4008895543",
    "data": [],
    "update": "1486536329",
    "cache": 7,
    "ord": "ASC"
}

JS decodeURIComponent 与 JAVA 解码_第1张图片
关注我的公众号.jpg

你可能感兴趣的:(JS decodeURIComponent 与 JAVA 解码)