Golang中的Json Unmarshal如何处理带指数的数字?

huangapple go评论83阅读模式
英文:

Golang Json Unmarshal numeric with exponent

问题

我在将json字符串解析为具有指数的数值时遇到了问题,它总是被解析为0。请检查下面的代码:

package main

import (
	"encoding/json"
	"fmt"
	"os"
)

type Person struct {
	Id   uint64 `json:"id"`
	Name string `json:"name"`
}

func main() {

	//创建Json字符串
	var b = []byte(`{"id": 1.2E+8, "Name": "Fernando"}`)

	//将Json解析为正确的结构体
	var f Person
	json.Unmarshal(b, &f)

	//打印Person结构体
	fmt.Println(f)

	//将结构体转换为Json
	result, _ := json.Marshal(f)

	//打印Json
	os.Stdout.Write(result)
}

运行结果为:

{0 Fernando}

有没有办法让它正常工作?因为指数是标准的JSON表示方式,看起来是Golang解析错误。

你可以在这里的playground中查看代码:http://play.golang.org/p/8owgjX9y0m

英文:

I have problem when Unmarshal json string into struct that is the numeric value with exponent will alway be 0.
Please check code below :

package main

import (
	"encoding/json"
	"fmt"
	"os"
)

type Person struct {
	Id   uint64  `json:"id"`
	Name string `json:"name"`
}

func main() {

	//Create the Json string
	var b = []byte(`{"id": 1.2E+8, "Name": "Fernando"}`)

	//Marshal the json to a proper struct
	var f Person
	json.Unmarshal(b, &f)

	//print the person
	fmt.Println(f)

	//unmarshal the struct to json
	result, _ := json.Marshal(f)

	//print the json
	os.Stdout.Write(result)
}

And the run is :

{0 Fernando}

Is there any way to make it work? Since the exponent thing is standart JSON. It seems the golang wrong interpret it.

Here the playground : http://play.golang.org/p/8owgjX9y0m

答案1

得分: 1

Id类型从int64更改为float32float64

http://play.golang.org/p/-zidTD_q8y

编辑:这可能有点取巧,但你可以添加一个类型为float64的“虚拟”Id字段,并编写一个钩子将该值转换为实际的Id类型int64

type Person struct {
    Id    float64          `json:"id"`
    _Id   int64             
    Name  string           `json:"name"`
}

var f Person
var b = []byte(`{"id": 1.2e+8, "Name": "Fernando"}`)
_ = json.Unmarshal(b, &f)

if reflect.TypeOf(f._Id) == reflect.TypeOf((int64)(0)) {
    fmt.Println(f.Id)
    f._Id = int64(f.Id)
}

http://play.golang.org/p/32HHLxnFlX

英文:

Change the Id type from int64 to float32 or float64.

http://play.golang.org/p/-zidTD_q8y

EDIT: This may be a bit of a hack, but you can add a "dummy" Id field of type float64 and write a hook to cast the value to the actual Id type int64.

type Person struct {
	Id    float64          `json:"id"`
	_Id   int64             
	Name  string           `json:"name"`
}

var f Person
var b = []byte(`{"id": 1.2e+8, "Name": "Fernando"}`)
_ = json.Unmarshal(b, &f)

if reflect.TypeOf(f._Id) == reflect.TypeOf((int64)(0)) {
	fmt.Println(f.Id)
	f._Id = int64(f.Id)
}

http://play.golang.org/p/32HHLxnFlX

答案2

得分: 1

只需将id字段的类型更改为float64。

package main
import (
    "encoding/json"
    "fmt"
    "os"
)

type Person struct {
    Id   float64  `json:"id"`
    Name string   `json:"name"`
}

func main() {

    //Create the Json string
    var b = []byte(`{"id": 1.2E+8, "Name": "Fernando"}`)

    //Marshal the json to a proper struct
    var f Person
    json.Unmarshal(b, &f)

    //print the person
    fmt.Println(f)

    //unmarshal the struct to json
    result, _ := json.Marshal(f)

    //print the json
    os.Stdout.Write(result)
}
英文:

just change the type of id field into float64.

package main
import (
    "encoding/json"
    "fmt"
    "os"
)

type Person struct {
    Id   float64  `json:"id"`
    Name string   `json:"name"`
}

func main() {

    //Create the Json string
    var b = []byte(`{"id": 1.2E+8, "Name": "Fernando"}`)

    //Marshal the json to a proper struct
    var f Person
    json.Unmarshal(b, &f)

    //print the person
    fmt.Println(f)

    //unmarshal the struct to json
    result, _ := json.Marshal(f)

    //print the json
    os.Stdout.Write(result)
}

huangapple
  • 本文由 发表于 2016年4月3日 13:37:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/36381997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定