JSON unmarshaling mit langen Nummern gibt Gleitkommazahl

War ich marshaling und unmarshaling JSONs mit golang und wenn ich will, es zu tun mit der Anzahl Felder golang verwandelt es in floating-point-zahlen anstelle von langen Nummern, zum Beispiel.

Habe ich folgenden JSON:

{
    "id": 12423434, 
    "Name": "Fernando"
}

Nach marshal es zu einer Karte und unmarshal wieder einen json-string, den ich bekommen:

{
    "id":1.2423434e+07,
    "Name":"Fernando"
}

Wie Sie sehen können die "id" Feld ist in Gleitkommadarstellung.

Den code, den ich verwende, ist folgende:

package main

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

func main() {

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

    //Marshal the json to a map
    var f interface{}
    json.Unmarshal(b, &f)
    m := f.(map[string]interface{})

    //print the map
    fmt.Println(m)

    //unmarshal the map to json
    result,_:= json.Marshal(m)

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

}

Er druckt:

map[id:1.2423434e+07 Name:Fernando]
{"Name":"Fernando","id":1.2423434e+07}

Scheint es zu sein, dass die erste marshal auf der Karte erzeugt der FP. Wie kann ich es beheben zu lange?

Dies ist der link zu dem Programm in der goland-Spielplatz:
http://play.golang.org/p/RRJ6uU4Uw-

  • Nicht der Marschall in einem map[string]interface{} aber eine richtige Struktur mit z.B. einem int64 Feld für id.
InformationsquelleAutor Fersca | 2014-03-12
Schreibe einen Kommentar