Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

json - How do you modify this struct in Golang to accept two different results?

For the following JSON response {"table_contents":[{"id":100,"description":"text100"},{"id":101,"description":"text101"},{"id":1,"description":"text1"}]}

All you have to do is to produce the following code to execute it properly and be able to reads fields from the struct, such as:

package main

import (
    "fmt"
    "encoding/json"
)

type MyStruct1 struct {
    TableContents []struct {
        ID          int
        Description string
    } `json:"table_contents"`
}

func main() {
    result:= []byte(`{"table_contents":[{"id":100,"description":"text100"},{"id":101,"description":"text101"},{"id":1,"description":"text1"}]}`)
    var container MyStruct1
    err := json.Unmarshal(result, &container)
    if err != nil {
        fmt.Println(" [0] Error message: " + err.Error())
        return
    }
    
    for i := range container.TableContents {
        fmt.Println(container.TableContents[i].Description)
    }
    
}

But how do you deal with the following JSON response? {"table_contents":[[{"id":100,"description":"text100"},{"id":101,"description":"text101"}],{"id":1,"description":"text1"}]} You can either get this response or the one above, it is important to modify the struct to accept both.

I did something like this, with the help of internet:

package main

import (
    "fmt"
    "encoding/json"
)

type MyStruct1 struct {
    TableContents []TableContentUnion `json:"table_contents"`
}

type TableContentClass struct {
    ID          int        
    Description string
}

type TableContentUnion struct {
    TableContentClass      *TableContentClass
    TableContentClassArray []TableContentClass
}

func main() {
    result:= []byte(`{"table_contents":[[{"id":100,"description":"text100"},{"id":101,"description":"text101"}],{"id":1,"description":"text1"}]}`)
    var container MyStruct1
    err := json.Unmarshal(result, &container)
    if err != nil {
        fmt.Println(" [0] Error message: " + err.Error())
        return
    }
    
    for i := range container.TableContents {
        fmt.Println(container.TableContents[i])
    }
    
}

but it does not go past the error message :(

  • [0] Error message: json: cannot unmarshal array into Go struct field MyStruct1.table_contents of type main.TableContentUnion*

Been struggling to come up with a solution for hours. If someone could help I would be happy. Thank you for reading. Let me know if you have questions

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...