douhan4093 2017-12-20 17:13
浏览 168
已采纳

Golang优雅地JSON解码不同的结构

I have different structures that share a field and I need to decode a JSON file into its corresponding structure in Go.

Example:

type Dog struct {
  AnimalType string //will always be "dog"
  BarkLoudnessLevel int
}

type Cat struct {
  AnimalType string //will always be "cat"
  SleepsAtNight bool
}

If I am receiving one of these structures as a JSON string, what would be the most elegant way of parsing it into its proper structure?

  • 写回答

1条回答 默认 最新

  • duanleixun2439 2017-12-20 17:40
    关注

    So, there are a couple ways of doing this, but the easiest is probably deserializing the payload twice and having conditional branches based off of the "AnimalType" attribute in your payload. Here's a simple example using an intermediate deserialization model:

    package main
    
    import (
      "fmt"
      "encoding/json"
    )
    
    type Dog struct {
      AnimalType string //will always be "dog"
      BarkLoudnessLevel int
    }
    
    type Cat struct {
      AnimalType string //will always be "cat"
      SleepsAtNight bool
    }
    
    var (
      payloadOne = `{"AnimalType":"dog","BarkLoudnessLevel":1}`
      payloadTwo = `{"AnimalType":"cat","SleepsAtNight":false}`
    )
    
    func main() {
      parseAnimal(payloadOne)
      parseAnimal(payloadTwo)
    }
    
    func parseAnimal(payload string) {
      animal := struct{
        AnimalType string
      }{} 
      if err := json.Unmarshal([]byte(payload), &animal); err != nil {
        panic(err)
      }
      switch animal.AnimalType {
      case "dog":
        dog := Dog{}
        if err := json.Unmarshal([]byte(payload), &dog); err != nil {
          panic(err)
        }
        fmt.Printf("Got a dog: %v
    ", dog)
      case "cat":
        cat := Cat{}
        if err := json.Unmarshal([]byte(payload), &cat); err != nil {
          panic(err)
        }
        fmt.Printf("Got a cat: %v
    ", cat)
      default:
        fmt.Println("Unknown animal")
      }
    }
    

    See it in action here.


    IMO a better way of approaching this is moving the "metadata" for the payload into a parent structure, though this requires modifying the expected json payload. So, for example, if you were working with payloads that looked like:

    {"AnimalType":"dog", "Animal":{"BarkLoudnessLevel": 1}}
    

    Then you could use something like json.RawMessage to partially parse the structure and then conditionally parse the rest as needed (rather than parsing everything twice)--also results in a nicer separation of structure attributes. Here's an example of how you'd do that:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type Animal struct {
        AnimalType string
        Animal     json.RawMessage
    }
    
    type Dog struct {
        BarkLoudnessLevel int
    }
    
    type Cat struct {
        SleepsAtNight bool
    }
    
    var (
        payloadOne = `{"AnimalType":"dog", "Animal":{"BarkLoudnessLevel": 1}}`
        payloadTwo = `{"AnimalType":"cat", "Animal":{"SleepsAtNight": false}}`
    )
    
    func main() {
        parseAnimal(payloadOne)
        parseAnimal(payloadTwo)
    }
    
    func parseAnimal(payload string) {
        animal := &Animal{}
        if err := json.Unmarshal([]byte(payload), &animal); err != nil {
            panic(err)
        }
        switch animal.AnimalType {
        case "dog":
            dog := Dog{}
            if err := json.Unmarshal(animal.Animal, &dog); err != nil {
                panic(err)
            }
            fmt.Printf("Got a dog: %v
    ", dog)
        case "cat":
            cat := Cat{}
            if err := json.Unmarshal(animal.Animal, &cat); err != nil {
                panic(err)
            }
            fmt.Printf("Got a cat: %v
    ", cat)
        default:
            fmt.Println("Unknown animal")
        }
    }
    

    And in action here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?