2017-11-20 10 views
-2

私はちょっとプログラミングに新しいが、私はpython didntがスピードを持っていることがわかったので、私はscraperをビルドして、私はASCII形式の文字列になるように見えるものに変換する必要があった。 jsonしかし、私は行くことでこれを行うにはどのような良い文書を見つけることができません。ASCII to Json go

変換される必要がある文字列は次のようになります:debug%22%3Afalse%2C%22pageOpts%22%3A%7B%22noBidIfUnsold%22%3true%2C%22%22%3A%7B%22no-sno- 22na-sno-finn-car_make%22%3A%22796%22%2C%22aa-sch-publisher%22%3A%22finn%22%2C%22aa- 22-A-C-22%A-22%A-22%A-22%A-22%A-22%A- sno-finn-ad_owner%22%3A%22false%22%2C%22no-sno-publishergroup%22%3A%22schsted%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%22オブジェクト%22%7D

ありがとうございます!

+7

ASCIIは文字エンコーディングであり、フォーマットではありません。その文字列はURLエンコードされているように見えます。デコードされると、JSONのように見えますが、有効なJSONドキュメントではありません。 – Adrian

+0

ありがとう!私が言ったように、これはちょっと新しい –

+0

"ASCII形式の文字列"は事ではありません。 – Flimzy

答えて

2

コメンターで述べたように、あなたの文字列が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(...)を使用して構造体またはマップにデコードすることができます。