簡體   English   中英

在 Go 中解析 json 時保留 int64 值

[英]preserve int64 values when parsing json in Go

我正在 Go 中處理一個 json POST,其中包含一個包含 64 位整數的對象數組。 使用 json.Unmarshal 時,這些值似乎被轉換為 float64,這不是很有幫助。

body := []byte(`{"tags":[{"id":4418489049307132905},{"id":4418489049307132906}]}`)

var dat map[string]interface{}
if err := json.Unmarshal(body, &dat); err != nil {
    panic(err)
}

tags := dat["tags"].([]interface{})

for i, tag := range tags {

    fmt.Println("tag: ", i, " id: ", tag.(map[string]interface{})["id"].(int64))

}

有沒有辦法在 json.Unmarshal 的輸出中保留原始的 int64?

轉到上述代碼的游樂場

解決方案1

您可以使用DecoderUseNumber無損地解碼您的數字:

Number類型定義如下:

// A Number represents a JSON number literal.
type Number string

這意味着您可以輕松轉換它:

package main

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

func main() {
    body := []byte("{\"tags\":[{\"id\":4418489049307132905},{\"id\":4418489049307132906}]}")
    dat := make(map[string]interface{})
    d := json.NewDecoder(bytes.NewBuffer(body))
    d.UseNumber()
    if err := d.Decode(&dat); err != nil {
        panic(err)
    }
    tags := dat["tags"].([]interface{})
    n := tags[0].(map[string]interface{})["id"].(json.Number)
    i64, _ := strconv.ParseUint(string(n), 10, 64)
    fmt.Println(i64) // prints 4418489049307132905
}

解決方案2

您還可以解碼為適合您需求的特定結構:

package main

import (
    "encoding/json"
    "fmt"
)

type A struct {
    Tags []map[string]uint64 // "tags"
}

func main() {
    body := []byte("{\"tags\":[{\"id\":4418489049307132905},{\"id\":4418489049307132906}]}")
    var a A
    if err := json.Unmarshal(body, &a); err != nil {
        panic(err)
    }
    fmt.Println(a.Tags[0]["id"]) // logs 4418489049307132905
}

就個人而言,我通常更喜歡這種感覺更有條理且更易於維護的解決方案。

警告

如果您使用 JSON,請注意一點,因為您的應用程序部分使用 JavaScript:JavaScript 沒有 64 位整數,而只有一種數字類型,即 IEEE754 雙精度浮點數。 因此,您將無法使用標准解析函數在 JavaScript 中解析此 JSON 而不丟失。

更簡單的一個:

body := []byte(`{"tags":[{"id":4418489049307132905},{"id":4418489049307132906}]}`)

var dat map[string]interface{}
if err := json.Unmarshal(body, &dat); err != nil {
    panic(err)
}

tags := dat["tags"].([]interface{})

for i, tag := range tags {
    fmt.Printf("tag: %v, id: %.0f", i, tag.(map[string]interface{})["id"].(float64))
}

我意識到這很舊,但這是我最終使用的解決方案

/* 
   skipping previous code, this is just converting the float 
   to an int, if the value is the same with or without what's 
   after the decimal points
*/

f := tag.(map[string]interface{})["id"].(float64)
if math.Floor(f) == f {
  fmt.Println("int tag: ", i, " id: ", int64(f))    
} else {
  fmt.Println("tag: ", i, " id: ", f)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM