GO語言"encoding/json"包中"Decoder.Token"類型的用法及代碼示例。
用法:
func(dec *Decoder) Token()(Token, error)
令牌返回輸入流中的下一個 JSON 令牌。在輸入流結束時,Token 返回 nil,io.EOF。
Token 保證它返回的分隔符 [ ] { } 正確嵌套和匹配:如果 Token 在輸入中遇到意外的分隔符,它將返回錯誤。
輸入流由基本的 JSON 值(bool、string、number 和 null)以及 Delim 類型的分隔符 [ ] { } 組成,用於標記數組和對象的開始和結束。省略了逗號和冒號。
例子:
此示例使用解碼器來解碼不同 JSON 值的流。
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"strings"
)
func main() {
const jsonStream = `
{"Message": "Hello", "Array": [1, 2, 3], "Null": null, "Number": 1.234}
`
dec := json.NewDecoder(strings.NewReader(jsonStream))
for {
t, err := dec.Token()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("%T: %v", t, t)
if dec.More() {
fmt.Printf(" (more)")
}
fmt.Printf("\n")
}
}
輸出:
json.Delim: { (more) string: Message (more) string: Hello (more) string: Array (more) json.Delim: [ (more) float64: 1 (more) float64: 2 (more) float64: 3 json.Delim: ] (more) string: Null (more) <nil>: <nil> (more) string: Number (more) float64: 1.234 json.Delim: }
相關用法
- GO Decoder.Decode用法及代碼示例
- GO Decoder用法及代碼示例
- GO DecodeLastRuneInString用法及代碼示例
- GO DecodeRuneInString用法及代碼示例
- GO DecodeRune用法及代碼示例
- GO Decode用法及代碼示例
- GO DecodeLastRune用法及代碼示例
- GO DecodeString用法及代碼示例
- GO DumpResponse用法及代碼示例
- GO DB.QueryRowContext用法及代碼示例
- GO Date用法及代碼示例
- GO DB.ExecContext用法及代碼示例
- GO Dial用法及代碼示例
- GO DB.BeginTx用法及代碼示例
- GO DumpRequest用法及代碼示例
- GO Drawer用法及代碼示例
- GO Duration.Hours用法及代碼示例
- GO Duration.Round用法及代碼示例
- GO DumpRequestOut用法及代碼示例
- GO Duration用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Decoder.Token。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。