Created
October 1, 2016 05:35
-
-
Save luizperes/8dce1dd11b97972f21a01d7dc2c61fef to your computer and use it in GitHub Desktop.
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 ( | |
"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