Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
997 views
in Technique[技术] by (71.8m points)

json - How do I make raw unicode encoded content readable?

I used net/http request a web API and the server returned a JSON response. When I print the response body, it displayed as raw ASCII content. I tried using bufio.ScanRunes to parse the content but failed.

I also tried write a simple server and return a unicode string and it worked well.

Here is the core code:

func (c ClientInfo) Request(method string, url string, form url.Values) string {
    req, _ := http.NewRequest(method, url, strings.NewReader(c.Encode(form)))
    req.Header = c.Header
    req.AddCookie(&c.Cookie)
    resp, err := http.DefaultClient.Do(req)
    defer resp.Body.Close()
    if err != nil {
        fmt.Println(err)
    }

    scanner := bufio.NewScanner(resp.Body)
    scanner.Split(bufio.ScanRunes)

    var buf bytes.Buffer
    for scanner.Scan() {
        buf.WriteString(scanner.Text())
    }
    rv := buf.String()
    fmt.Println(rv)
    return rv
}

Here is the example output:

{"forum":{"id":"3251718","name":"u5408u80a5u5de5u4e1au5927u5b66u5ba3u57ceu6821u533a","first_class":"u9ad8u7b49u9662u6821","second_class":"u5b89u5fbdu9662u6821","is_like":"0","user_level":"1","level_id":"1","level_name":"u7d20u672au8c0bu9762","cur_score":"0","levelup_score":"5","member_num":"80329","is_exists":"1","thread_num":"108762","post_num":"3445881","good_classify":[{"class_id":"0","class_name":"u5168u90e8"},{"class_id":"1","class_name":"u516cu544au7c7b"},{"class_id":"2","class_name":"u5427u53cbu4e13u533a"},{"class_id":"4","class_name":"u6d3bu52a8u4e13u533a"},{"class_id":"6","class_name":"u793eu56e2u73edu7ea7"},{"class_id":"5","class_name":"u8d44u6e90u5171u4eab"},{"class_id":"8","class_name":"u6e29u99a8u751fu6d3bu7c7b"},{"class_id":"7","class_name":"u54a8u8be2u65b0u95fbu7c7b"},{"class_id":"3","class_name":"u98ceu91c7u5c55u793au533a"}],"managers":[{"id":"793092593","name":"yiu62b9u660eu5a9au7684u5fe7u4f24"},

...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...