Skip to content

Instantly share code, notes, and snippets.

@reusee
Last active November 15, 2018 02:39
Show Gist options
  • Save reusee/2cf9de7bbf46e6049a798630002db54b to your computer and use it in GitHub Desktop.
Save reusee/2cf9de7bbf46e6049a798630002db54b to your computer and use it in GitHub Desktop.
malformed.go
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