Created
February 26, 2015 21:42
-
-
Save jsclayton/23f2ae1c16b9670493c2 to your computer and use it in GitHub Desktop.
Encode & decode a JSON string as a number in 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" | |
) | |
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