如何解码URIComponent?(例如:“%2F%3F%26%3D”->“/?&=”)

Ser*_*sev 0 m powerquery powerbi

如何转换这个

qqqq%2Fwwww%3Feeee%26rrrr%3Dtttt
Run Code Online (Sandbox Code Playgroud)

对此

qqqq/wwww?eeee&rrrr=tttt
Run Code Online (Sandbox Code Playgroud)

?

有没有简单的解码方法?

Car*_*lsh 5

我们有一个 URI编码库函数,Uri.EscapeDataString但我们没有要解码的逆(这可能是一个疏忽)。请随时在https://ideas.powerbi.com上建议此功能


如果有任何非 ASCII 字符,则编写自己的文本比用“%”替换文本要复杂一些。

这是一个 M 实现,它构建字节然后将文本转换为 UTF-8 二进制:

let
    InputData = Csv.Document("a=b
=ab
ab=
abc?def"),

    Uri.UnescapeDataString = (data as text) as text => let 
        ToList = Text.ToList(data),
        Accumulate = List.Accumulate(ToList, [ Bytes = {} ], (state, current) => 
            let
                HexString = state[HexString]?,
                NextHexString = HexString & current,
                NextState = if HexString <> null
                  then if Text.Length(NextHexString) = 2
                      then [ Bytes = state[Bytes] & Binary.ToList(Binary.FromText(NextHexString, BinaryEncoding.Hex)) ]
                      else [ HexString = NextHexString, Bytes = state[Bytes] ]
                  else if current = "%"
                      then [ HexString = "", Bytes = state[Bytes] ]
                  else [ Bytes = state[Bytes] & { Character.ToNumber(current) } ]
            in
                NextState),
        FromBinary = Text.FromBinary(Binary.FromList(Accumulate[Bytes]))
      in
        FromBinary,

    AddEscaped = Table.AddColumn(InputData, "Escaped", each Uri.EscapeDataString([Column1])),

    AddUnescaped = Table.AddColumn(AddEscaped, "Custom", each Uri.UnescapeDataString([Escaped]))
in
    AddUnescaped
Run Code Online (Sandbox Code Playgroud)

(我为上述感到自豪,但我想到了一种更简单的方法,如果您知道所有数据都已正确编码。)

您可以将字符串连接成一个 URL 并利用Uri.Parts类似的 URL 解码功能:

Uri.UnescapeDataString = (data as text) as text => 
    Uri.Parts("http://whatever?a=" & data)[Query][a],
Run Code Online (Sandbox Code Playgroud)