gpt4 book ai didi

json - 去 Json 的 ASCII

转载 作者:数据小太阳 更新时间:2023-10-29 03:39:55 25 4
gpt4 key购买 nike

我对编程有点陌生,但我发现 python 没有我需要的速度,所以我转而去,我正在构建一个抓取器,我需要将一个看起来像是 ASCII 格式的字符串转换为 json,但我找不到关于如何在 go 中执行此操作的任何好的文档。

我需要转换的字符串看起来像这样:debug%22%3Afalse%2C%22pageOpts%22%3A%7B%22noBidIfUnsold%22%3Atrue%2C%22keywords%22%3A%7B%22no-sno-finn -object_type%22%3A%22private%22%2C%22no-sno-finn-car_make%22%3A%22796%22%2C%22aa-sch-publisher%22%3A%22finn%22%2C%22aa-sch -inventory_type%22%3A%22classified%22%2C%22aa-sch-country_code%22%3A%22no%22%2C%22no-sno-finn-section%22%3A%22car%22%2C%22no-sno -finn-ad_owner%22%3A%22false%22%2C%22no-sno-publishergroup%22%3A%22schibsted%22%2C%22aa-sch-supply_type%22%3A%22web_desktop%22%2C%22no-sno -finn-subsection%22%3A%22car_used%22%2C%22aa-sch-page_type%22%3A%22object%22%7D

提前致谢!

最佳答案

正如评论者所提到的,您的字符串是 URL 编码的,可以使用 url.QueryUnescape(...) 进行解码。 :

package main

import (
"fmt"
"net/url"
)

func main() {
querystr := "debug%22%3Afalse%2C%22pageOpts%22%3A%7B%22noBidIfUnsold%22%3Atrue%2C%22keywords%22%3A%7B%22no-sno-finn-object_type%22%3A%22private%22%2C%22no-sno-finn-car_make%22%3A%22796%22%2C%22aa-sch-publisher%22%3A%22finn%22%2C%22aa-sch-inventory_type%22%3A%22classified%22%2C%22aa-sch-country_code%22%3A%22no%22%2C%22no-sno-finn-section%22%3A%22car%22%2C%22no-sno-finn-ad_owner%22%3A%22false%22%2C%22no-sno-publishergroup%22%3A%22schibsted%22%2C%22aa-sch-supply_type%22%3A%22web_desktop%22%2C%22no-sno-finn-subsection%22%3A%22car_used%22%2C%22aa-sch-page_type%22%3A%22object%22%7D"

// Parse the URL encoded string.
plainstr, err := url.QueryUnescape(querystr)
if err != nil {
panic(err)
}
fmt.Println(plainstr)
// debug":false,"pageOpts":{"noBidIfUnsold":true,"keywords":{"no-sno-finn-object_type":"private","no-sno-finn-car_make":"796","aa-sch-publisher":"finn","aa-sch-inventory_type":"classified","aa-sch-country_code":"no","no-sno-finn-section":"car","no-sno-finn-ad_owner":"false","no-sno-publishergroup":"schibsted","aa-sch-supply_type":"web_desktop","no-sno-finn-subsection":"car_used","aa-sch-page_type":"object"}

}

您的示例字符串似乎不完整,但最终可以使用 json.Unmarshal(...) 将其解码为结构或映射。 .

关于json - 去 Json 的 ASCII,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47398647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com