我需要一个JSON字符串,如浮点数解码:如何解码JSON的类型从字符串转换为Golang中的float64?
{"name":"Galaxy Nexus", "price":"3460.00"}
我用下面的Golang代码:
package main
import (
"encoding/json"
"fmt"
)
type Product struct {
Name string
Price float64
}
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)
} else {
fmt.Println(err)
fmt.Printf("%+v\n", pro)
}
}
当我运行它,得到的结果是:
json: cannot unmarshal string into Go value of type float64
{Name:Galaxy Nexus Price:0}
我想知道如何解码类型转换的JSON字符串。
谢谢!我认为这是解决我的问题的最佳解决方案。你能告诉我关于“,字符串”用法的官方文件在哪里? – yanunon 2012-03-06 02:35:05
我在[link](http://weekly.golang.org/pkg/encoding/json/)上找到了它。 – yanunon 2012-03-06 02:47:29
+1太棒了。我不知道这件事。 – Mostafa 2012-03-06 10:20:02