一。Decoder

 1 /(一)Decoder
 2 func DecoderExample(){
 3     const jsonStream = ` 
 4         { "Name" : "Ed" , "Text" : "Knock knock." } 
 5         { "Name" : "Sam" , "Text" : "Who's there?" } 
 6         { "Name" : "Ed" , "Text" : "Go fmt." } 
 7         { "Name" : "Sam" , "Text" : "Go fmt who?" } 
 8         { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
 9     `
10     type Message struct {
11         Name , Text string
12     }
13     dec := json. NewDecoder ( strings. NewReader ( jsonStream ) )
14     for {
15         var m Message
16         if err := dec. Decode ( & m ) ; err == io. EOF {
17             break
18         } else if err != nil {
19             log . Fatal ( err )
20         }
21         fmt. Printf ( "%s: %s \n " , m. Name , m. Text )
22         /*
23         执行结果:
24          Ed: Knock knock.
25          Sam: Who's there?
26          Ed: Go fmt.
27          Sam: Go fmt who?
28          Ed: Go fmt yourself!
29         */
30     }
31 }
View Code

相关文章:

  • 2021-10-12
  • 2022-12-23
  • 2021-11-05
  • 2021-10-19
  • 2018-02-08
猜你喜欢
  • 2021-04-02
  • 2019-06-02
  • 2022-12-23
  • 2022-01-18
  • 2021-11-05
相关资源
相似解决方案