Skip to content

Instantly share code, notes, and snippets.

@luizperes
Created October 1, 2016 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizperes/8dce1dd11b97972f21a01d7dc2c61fef to your computer and use it in GitHub Desktop.
Save luizperes/8dce1dd11b97972f21a01d7dc2c61fef to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
)
const (
READ_KEY = 1
READ_VALUE = -1
)
var (
dictionary map[string]string
json string = "{\"name\":\"luiz\",\"age\": \"26\"}"
)
func main() {
dictionary = make(map[string]string)
var state int = READ_KEY
var key string = ""
for i := 0; i < len(json); i++ {
switch json[i] {
case '"':
if (state == READ_KEY) {
i, key = getId(json, i+1)
} else {
i, dictionary[key] = getId(json, i+1)
fmt.Println(key + " : " + dictionary[key])
}
case ',':
state = READ_KEY
case ':':
state = READ_VALUE
case '}':
// exit the parser
return
default: break
}
}
}
func getId(json string, i int) (newI int, newId string) {
buff := bytes.NewBufferString(newId)
for newI = i ; newI < len(json) && json[newI] != '"'; newI++ {
buff.WriteByte (json[newI])
}
newId = buff.String()
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment