programing tip

Go 맵을 json으로 변환

itbloger 2020. 10. 22. 07:44
반응형

Go 맵을 json으로 변환


encoding/jsonMarshal 을 사용하여 Go 맵을 json 문자열로 변환하려고 했지만 결과적으로 빈 문자열이 생성되었습니다.

내 코드는 다음과 같습니다.

package main

import (
    "encoding/json"
    "fmt"
)

type Foo struct {
    Number int    `json:"number"`
    Title  string `json:"title"`
}

func main() {
    datas := make(map[int]Foo)

    for i := 0; i < 10; i++ {
        datas[i] = Foo{Number: 1, Title: "test"}
    }

    jsonString, _ := json.Marshal(datas)

    fmt.Println(datas)
    fmt.Println(jsonString)
}

내 출력은 다음과 같습니다.

map[9:{1 test} 2:{1 test} 7:{1 test} 3:{1 test} 4:{1 test} 5:{1 test} 6:{1 test} 8:{1 test} 0:{1 test} 1:{1 test}]

[]

나는 내가 어디에서 틀렸는 지 정말로 모른다. 도와 주셔서 감사합니다.


오류를 발견했다면 다음과 같이 보셨을 것입니다.

jsonString, err := json.Marshal(datas)
fmt.Println(err)

// [] json: unsupported type: map[int]main.Foo

문제는 JSON에서 정수를 키로 사용할 수 없다는 것입니다. 금지되어 있습니다. 대신 이러한 값을 사전에 문자열로 변환 할 수 있습니다 (예 : strconv.Itoa.

자세한 내용은이 게시물을 참조하십시오 : https://stackoverflow.com/a/24284721/2679935


실제로 무엇이 잘못되었는지 알려주지 만에서 반환 된 오류를 확인하지 않았기 때문에 무시했습니다 json.Marshal.

json: unsupported type: map[int]main.Foo

JSON 스펙은 객체 키에 대한 문자열을 제외하고는 아무것도 지원하지 않지만 자바 스크립트는 까다 롭지 않지만 여전히 불법입니다.

두 가지 옵션이 있습니다.

1 map[string]Foo인덱스를 사용 하고 문자열로 변환합니다 (예 : fmt. Sprint 사용).

datas := make(map[string]Foo, N)

for i := 0; i < 10; i++ {
    datas[fmt.Sprint(i)] = Foo{Number: 1, Title: "test"}
}
j, err := json.Marshal(datas)
fmt.Println(string(j), err)

2 간단히 슬라이스 (자바 스크립트 배열)를 사용합니다.

datas2 := make([]Foo, N)
for i := 0; i < 10; i++ {
    datas2[i] = Foo{Number: 1, Title: "test"}
}
j, err = json.Marshal(datas2)
fmt.Println(string(j), err)

playground


이 질문에 대한 질문 / 마지막 답변 이후, 여기 에서 TextMarshalerTextUnmarshaler 인터페이스를 사용하여 json Marshal / UnMarshal의 맵에 대한 비 문자열 키 유형에 대한 지원이 추가되었습니다 . 키 유형에 대해 이러한 인터페이스를 구현하면 예상대로 작동합니다.json.Marshal

package main

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

// Num wraps the int value so that we can implement the TextMarshaler and TextUnmarshaler 
type Num int

func (n *Num) UnmarshalText(text []byte) error {
    i, err := strconv.Atoi(string(text))
    if err != nil {
        return err
    }
    *n = Num(i)
    return nil
}

func (n Num) MarshalText() (text []byte, err error) {
    return []byte(strconv.Itoa(int(n))), nil
}

type Foo struct {
    Number Num    `json:"number"`
    Title  string `json:"title"`
}

func main() {
    datas := make(map[Num]Foo)

    for i := 0; i < 10; i++ {
        datas[Num(i)] = Foo{Number: 1, Title: "test"}
    }

    jsonString, err := json.Marshal(datas)
    if err != nil {
        panic(err)
    }

    fmt.Println(datas)
    fmt.Println(jsonString)

    m := make(map[Num]Foo)
    err = json.Unmarshal(jsonString, &m)
    if err != nil {
        panic(err)
    }

    fmt.Println(m)
}

산출:

map[1:{1 test} 2:{1 test} 4:{1 test} 7:{1 test} 8:{1 test} 9:{1 test} 0:{1 test} 3:{1 test} 5:{1 test} 6:{1 test}]
[123 34 48 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 49 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 50 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 51 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 52 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 53 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 54 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 55 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 56 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 57 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 125]
map[4:{1 test} 5:{1 test} 6:{1 test} 7:{1 test} 0:{1 test} 2:{1 test} 3:{1 test} 1:{1 test} 8:{1 test} 9:{1 test}]

참고 URL : https://stackoverflow.com/questions/24652775/convert-go-map-to-json

반응형