我需要一個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