2017-11-20 1 views
-2

메신저 좀 프로그래밍에 익숙하지만, 파이썬이 필요로하는 속도가 없다는 것을 알았습니다. 그래서 나는 스크래퍼를 만들었고 ASCII 형식의 문자열로 변환해야합니다. json하지만이 작업을 수행하는 방법에 대한 좋은 문서는 찾을 수 없습니다.ASCII에서 Json으로 이동

변환해야하는 문자열은 다음과 유사합니다 : 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- 22 % 3 % 22 % 3A % 22 분류 된 % 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 % 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(...)를 사용하여 구조체 또는지도로 디코딩 할 수있다.