Last active
November 15, 2018 02:39
-
-
Save reusee/2cf9de7bbf46e6049a798630002db54b to your computer and use it in GitHub Desktop.
malformed.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"strings" | |
) | |
func main() { | |
j := ` | |
{ | |
"foo": [ | |
1, 2, 3 | |
], | |
"foo": [ | |
4, 5, 6 | |
] | |
} | |
` | |
decoder := json.NewDecoder(strings.NewReader(j)) | |
_, err := decoder.Token() // 第一个 '{' | |
if err != nil { | |
panic("bad json") | |
} | |
for { | |
token, err := decoder.Token() // "foo" 或者 "}" | |
if err != nil { | |
panic("bad json") | |
} | |
if _, ok := token.(json.Delim); ok { | |
break | |
} | |
var array []int // 解码数组 | |
if err := decoder.Decode(&array); err != nil { | |
panic(err) | |
} | |
fmt.Printf("%v\n", array) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment