我用net/http
請求一個web API並且服務器返回了一個JSON響應。當我打印響應正文時,它顯示爲原始ASCII內容。我嘗試使用bufio.ScanRunes
解析內容但失敗。如何讓原始的unicode編碼內容可讀?
我也嘗試寫一個簡單的服務器,並返回一個Unicode字符串,它運作良好。
這裏是核心代碼:
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
}
下面是示例輸出:
{ 「論壇」:{ 「ID」: 「3251718」, 「名」:「\ u5408 \ u80a5 \ u5de5 \ u4e1a \ u5927 \ u5b66 \ u5ba3 \ u57ce \ u6821 \ u533a」, 「first_class」: 「\ u9ad8 \ u7b49 \ u9662 \ u6821」, 「second_class」: 「\ u5b89 \ u5fbd \ u9662 \ u6821」 「is_like」: 「0」, 「user_level」: 「1」, 「level_id」: 「1」, 「level_name」: 「\ u7d20 \ u672a \ u8c0b \ u9762」, 「cur_score」: 「0」, 「levelup_score」 : 「5」, 「member_num」: 「80329」, 「is_exists」: 「1」, 「thread_num」: 「108762」, 「post_num」: 「3445881」, 「good_classify」:[{ 「類標識碼」: 「0」 「CLASS_NAME」:「\ u5168 \ü 90e8 「},{」 類標識碼 「:」 1" , 「CLASS_NAME」: 「\ u516c \ u544a \ u7c7b」},{ 「類標識碼」: 「2」, 「CLASS_NAME」: 「\ u5427 \ u53cb \ u4e13 \ u533a」 },{ 「類標識碼」: 「4」, 「CLASS_NAME」: 「\ u6d3b \ u52a8 \ u4e13 \ u533a」},{ 「類標識碼」: 「6」, 「CLASS_NAME」: 「\ u793e \ u56e2 \ u73ed \ u7ea7」 },{ 「類標識碼」: 「5」, 「CLASS_NAME」: 「\ u8d44 \ u6e90 \ u5171 \ u4eab」},{ 「類標識碼」: 「8」, 「CLASS_NAME」:「\ u6e29 \ u99a8 \ u751f \ u6d3b \ u7c7b 「},{」 類標識碼 「:」 7" , 「CLASS_NAME」: 「\ u54a8 \ u8be2 \ u65b0 \ u95fb \ u7c7b」},{ 「類標識碼」: 「3」, 「CLASS_NAME」:「\ u98ce \ u91c7 \管理員「:[{」id「:」793092593「,」name「:」yi \ u62b9 \ u9a9a \ u7684 \ u5fe7 \ u4f24「},
.. 。
感謝您的回答。 – earlzo