将字符串转换为float64的接收格式为”.01″。

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

String to float64 receiving format ".01"

问题

如果我从一个API接收到一个遵循".01"格式的字符串,并且我有一个如下的结构体:

type Mystruct struct {
Val float64 json:"val,string"
}

在这种情况下,我尝试将val反序列化为float64。有没有办法可以实现这个?

英文:

If I receive from an API a string obeying the format of ".01", and I have a struct like this:

type Mystruct struct {
Val float64 json:"val,string"
}

In this case, I receive trying to unmarshal val into float64. Is there a way I can accomplish this?

答案1

得分: 1

添加一个字符串字段来捕获字符串值:

type Mystruct struct {
    Val  float64 `json:"-"`
    XVal string  `json:"val"`
}

解析JSON文档。将字符串值转换为浮点值:

var v Mystruct
err := json.Unmarshal([]byte(data), &v)
if err != nil {
    log.Fatal(err)
}
v.Val, err = strconv.ParseFloat(v.XVal, 64)
if err != nil {
    log.Fatal(err)
}
英文:

Add a string field to capture the string value:

type Mystruct struct {
	Val  float64 `json:"-"`
	XVal string  `json:"val"`
}

Unmarshal the JSON document. Convert the string value to a float value:

var v Mystruct
err := json.Unmarshal([]byte(data), &v)
if err != nil {
	log.Fatal(err)
}
v.Val, err = strconv.ParseFloat(v.XVal, 64)
if err != nil {
	log.Fatal(err)
}

答案2

得分: 1

我建议定义一个类型别名,你可以在任何地方使用它。

package main

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
)

type MyFloat64 float64

func (f *MyFloat64) UnmarshalJSON(data []byte) error {
	raw := string(data)
	raw = strings.TrimPrefix(raw, "\"")
	raw = strings.TrimSuffix(raw, "\"")
	if parsedFloat, err := strconv.ParseFloat(raw, 64); err != nil {
		return err
	} else {
		*f = MyFloat64(parsedFloat)
		return nil
	}
}

type MyObj struct {
	Val1 MyFloat64
	Val2 string
}

func main() {
	j := `{"Val1":"0.01", "Val2":"0.01"}`
	o := MyObj{}
	err := json.Unmarshal([]byte(j), &o)
	if err != nil {
		fmt.Println(err)
	} else {
		b, _ := json.Marshal(o)
		fmt.Println("in:", j)
		fmt.Println("out:", string(b))
	}
}

输出:

in: {"Val1":"0.01", "Val2":"0.01"}
out: {"Val1":0.01,"Val2":"0.01"}
英文:

I recommand defining a type alias which you can use it anywhere.

package main

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
)

type MyFloat64 float64

func (f *MyFloat64) UnmarshalJSON(data []byte) error {
	raw := string(data)
	raw = strings.TrimPrefix(raw, "\"")
	raw = strings.TrimSuffix(raw, "\"")
	if parsedFloat, err := strconv.ParseFloat(raw, 64); err != nil {
		return err
	} else {
		*f = MyFloat64(parsedFloat)
		return nil
	}
}

type MyObj struct {
	Val1 MyFloat64
	Val2 string
}

func main() {
	j := `{"Val1":"0.01", "Val2":"0.01"}`
	o := MyObj{}
	err := json.Unmarshal([]byte(j), &o)
	if err != nil {
		fmt.Println(err)
	} else {
		b, _ := json.Marshal(o)
		fmt.Println("in:", j)
		fmt.Println("out:", string(b))
	}
}

output:

in: {"Val1":"0.01", "Val2":"0.01"}
out: {"Val1":0.01,"Val2":"0.01"}

huangapple
  • 本文由 发表于 2021年10月10日 09:50:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/69511737.html
匿名

发表评论

匿名网友

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

确定