Cara Mengendalikan Kandungan Unikod Berkod
Apabila anda menemui kandungan ASCII mentah dalam respons JSON, ia mungkin dikodkan kandungan Unikod. Untuk melihat teks yang tidak dipetik, anda boleh menggunakan pakej json.
func main() { var i interface{} err := json.Unmarshal([]byte(src), &i) fmt.Println(err, i) } const src = `{"forum":{"id":"3251718","name":"\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":[{"class_id":"0","class_name":"\u5168\u90e8"},{"class_id":"1","class_name":"\u516c\u544a\u7c7b"},{"class_id":"2","class_name":"\u5427\u53cb\u4e13\u533a"},{"class_id":"4","class_name":"\u6d3b\u52a8\u4e13\u533a"},{"class_id":"6","class_name":"\u793e\u56e2\u73ed\u7ea7"},{"class_id":"5","class_name":"\u8d44\u6e90\u5171\u4eab"},{"class_id":"8","class_name":"\u6e29\u99a8\u751f\u6d3b\u7c7b"},{"class_id":"7","class_name":"\u54a8\u8be2\u65b0\u95fb\u7c7b"},{"class_id":"3","class_name":"\u98ce\u91c7\u5c55\u793a\u533a"}]}}`
Output:
<nil> map[forum:map[levelup_score:5 is_exists:1 post_num:3445881 good_classify:[map[class_id:0 class_name:全部] map[class_id:1 class_name:公告类] map[class_id:2 class_name:吧友专区] map[class_id:4 class_name:活动专区] map[class_id:6 class_name:社团班级] map[class_id:5 class_name:资源共享] map[class_id:8 class_name:温馨生活类] map[class_name:咨询新闻类 class_id:7] map[class_id:3 class_name:风采展示区]] id:3251718 is_like:0 cur_score:0
Jika anda memilih untuk menyahpetik serpihan tertentu, gunakan strconv.Unquote():
fmt.Println(strconv.Unquote(`"\u7d20\u672a\u8c0b"`))
Output:
素未谋 <nil>
Nota bahawa strconv.Unquote() memerlukan rentetan dalam petikan. Literal rentetan mentah boleh digunakan untuk melampirkan rentetan sedemikian, menghalang pengkompil daripada mentafsir Unicode melarikan diri.
Atas ialah kandungan terperinci Bagaimana Mengendalikan Kandungan Unicode Berkod dalam JSON Menggunakan Go?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!