我是golang原型的新手。我需要解密从查询参数中获取的字符串。
这是我的绳子。

%7B%22predicates%22%3A%5B%7B%22key%22%3A%22name%22%2C%22op%22%3A%22IS_SUBSTRING%22%2C%22string_value%22%3A%22123%22%7D%2C%7B%22key%22%3A%22storage_state%22%2C%22op%22%3A%22NOT_EQUALS%22%2C%22string_value%22%3A%22STORAGESTATE_ARCHIVED%22%7D%5D%7D
我找不到如何解密此字符串。
谁能提供给我一个使用golang解密此字符串的想法
提前致谢

最佳答案

您可以使用std lib中的url.PathUnescape(s)(尝试online):

package main

import (
    "fmt"
    "log"
    "net/url"
)

func main() {
    s := `%7B%22predicates%22%3A%5B%7B%22key%22%3A%22name%22%2C%22op%22%3A%22IS_SUBSTRING%22%2C%22string_value%22%3A%22123%22%7D%2C%7B%22key%22%3A%22storage_state%22%2C%22op%22%3A%22NOT_EQUALS%22%2C%22string_value%22%3A%22STORAGESTATE_ARCHIVED%22%7D%5D%7D`
    st, err := url.PathUnescape(s)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(st)
}

输出:
{"predicates":[{"key":"name","op":"IS_SUBSTRING","string_value":"123"},{"key":"storage_state","op":"NOT_EQUALS","string_value":"STORAGESTATE_ARCHIVED"}]}
您可以Unmarshal:
    var m map[string]interface{}
    err = json.Unmarshal([]byte(st), &m)
    if err != nil {
        log.Fatal(err)
    }
decode:
    d := json.NewDecoder(strings.NewReader(st))
    var m map[string]interface{}
    err = d.Decode(&m)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(m)

关于go - 从URL查询参数解码JSON,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63610820/

10-12 20:49