Skip to content

Instantly share code, notes, and snippets.

@jsclayton
Created February 26, 2015 21:42
Show Gist options
  • Save jsclayton/23f2ae1c16b9670493c2 to your computer and use it in GitHub Desktop.
Save jsclayton/23f2ae1c16b9670493c2 to your computer and use it in GitHub Desktop.
Encode & decode a JSON string as a number in Go
package main
import (
"encoding/json"
"fmt"
)
type Product struct {
Name string
Price float64 `json:",string"`
}
func main() {
s := `{"name":"Galaxy Nexus", "price":"3460.00"}`
var pro Product
err := json.Unmarshal([]byte(s), &pro)
if err == nil {
fmt.Printf("%+v\n", pro)
// https://play.golang.org/p/GDrP18janS
// http://stackoverflow.com/questions/9452897/how-to-decode-json-with-type-convert-from-string-to-float64-in-golang
proJson, _ := json.Marshal(pro)
fmt.Printf(string(proJson))
} else {
fmt.Println(err)
fmt.Printf("%+v\n", pro)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment