skip to Main Content

I am learning https://www.digitalocean.com/community/tutorials/how-to-use-json-in-go#using-a-struct-to-generate-json (old version of Go).

I use go 1.20.1 , Windows 11 x64, GoLand 2022.3.2 .

package sample3

import (
    foo "encoding/json"
    "fmt"
    "time"
)

type myJSON struct {
    IntValue        int       `json:"intValue"`
    BoolValue       bool      `json:"boolValue"`
    StringValue     string    `json:"stringValue"`
    DateValue       time.Time `json:"dateValue"`
    ObjectValue     *myObject `json:"objectValue"`
    NullStringValue *string   `json:"nullStringValue"`
    NullIntValue    *int      `json:"nullIntValue"`
}

type myObject struct {
    ArrayValue []int `json:"arrayValue"`
}

func Main3() {
    otherInt := 4321
    data := &myJSON{
        IntValue:    1234,
        BoolValue:   true,
        StringValue: "hello!",
        DateValue:   time.Date(2022, 3, 2, 9, 10, 0, 0, time.UTC),
        ObjectValue: &myObject{
            ArrayValue: []int{1, 2, 3, 4},
        },
        NullStringValue: nil,
        NullIntValue:    &otherInt,
    }
    fmt.Println(foo.Marshal(data))
    fmt.Println(data)

    type myInt struct {
        IntValue int
    }

    data2 := &myInt{IntValue: 1234}
    fmt.Println(foo.Marshal(data2))

}

line

fmt.Println(foo.Marshal(data))

return

&{1234 true hello! 2022-03-02 09:10:00 +0000 UTC 0xc000008240 <nil> 0xc00001a170}

enter image description here

enter image description here

I want to see {"IntValue": 1234, "BoolValue": true, ...} , please guide me.

full source code https://github.com/donhuvy/vy_learn_go_json2023/blob/main/sample3/main3.go#L36

enter image description here

why I use fmt.Println(string(json.Marshal(data))) causes error?

2

Answers


  1. I usually use json encoding library. Take a look at below example:

    package main
    
    import (
        "encoding/json"
        "time"
    )
    
    type myJSON struct {
        IntValue        int       `json:"intValue"`
        BoolValue       bool      `json:"boolValue"`
        StringValue     string    `json:"stringValue"`
        DateValue       time.Time `json:"dateValue"`
        ObjectValue     *myObject `json:"objectValue"`
        NullStringValue *string   `json:"nullStringValue"`
        NullIntValue    *int      `json:"nullIntValue"`
    }
    
    type myObject struct {
        ArrayValue []int `json:"arrayValue"`
    }
    
    func main() {
        otherInt := 4321
        data := &myJSON{
            IntValue:    1234,
            BoolValue:   true,
            StringValue: "hello!",
            DateValue:   time.Date(2022, 3, 2, 9, 10, 0, 0, time.UTC),
            ObjectValue: &myObject{
                ArrayValue: []int{1, 2, 3, 4},
            },
            NullStringValue: nil,
            NullIntValue:    &otherInt,
        }
        bytes, err := json.Marshal(data)   // <-------------------This line
        println(string(bytes)) // <-------------------And this line
        println(err)
    }
    

    Output:

    {"intValue":1234,"boolValue":true,"stringValue":"hello!","dateValue":"2022-03-02T09:10:00Z","objectValue":{"arrayValue":[1,2,3,4]},"nullStringValue":null,"nullIntValue":4321}
    
    Login or Signup to reply.
  2. You have not formatter your output properly

    do as follows, for detail on fmt check here
    go_fmt

        d, _ := foo.Marshal(data)
        fmt.Printf("%+v n", string(d))
        fmt.Printf("%+v n", *data)
    
        type myInt struct {
            IntValue int
        }
    
        data2 := &myInt{IntValue: 1234}
        d2, _ := foo.Marshal(data2)
        fmt.Printf("%+v", string(d2))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search