douyi7283
2017-02-01 23:52
浏览 105

Golang中的Json Unmarshal类型模型

I have a RESTful service that returns response similar to show below:

"Basket" : {
  "Count": 1,
  "Fruits": {[
    {
      "Name":"Mango", 
      "Season":"Summer"
    },
    {
      "Name":"Fig", 
      "Season":"Winter"}
  ]}
}

I am trying to create Go lang model to unmarshal the contents. Following is the code I have tried:

type Response struct {
    Count   int
    Fruits []Fruit
}

type Fruit struct {
    Name string
    Season string
}

But when I marshal the Response object in my test code I don't see similar json. (https://play.golang.org/p/EGKqfbwFvW) Marshalled data always appears as :

{
  "Count":100,
  "Fruits":[
    {"Name":"Mango","Season":"Summer"},
    {"Name":"Fig","Season":"Winter"}
  ]
}

Notice the Fruits appearing as array [] and not {[]} in original json. How can I model structs in golang for this response?

图片转代码服务由CSDN问答提供 功能建议

我有一个RESTful服务,返回的响应类似于以下所示:

  “购物篮”:{
“ Count”:1,
“ Fruits”:{[
 {
“ Name”:“ Mango”,
“ Season”:“ Summer” 
},  
 {
“ Name”:“ Fig”,
“ Season”:“ Winter”} 
]} 
} 
   
 
 

我正在尝试 创建Go lang模型以解组内容。 以下是我尝试过的代码:

  type响应结构{
计数int 
水果[] Fruit 
} 
 
type水果结构{
名称字符串 
季节字符串
} 
   
 
 

但是当我在测试代码中封送Response对象时,看不到类似的json。 ( https://play.golang.org/p/EGKqfbwFvW ) 总是会出现编组数据 如:

  {
“ Count”:100,
“ Fruits”:[
 {“ Name”:“ Mango”,“ Season”:“ Summer”  },
 {“ Name”:“ Fig”,“ Season”:“ Winter”} 
] 
} 
   
 
 

注意,水果以数组形式出现 [] ,而不是原始json中的 {[]} 。 如何为此响应在golang中建模结构?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doushijiao0679 2017-02-02 14:43
    已采纳

    Your model is totally correct and valid, but the JSON object is not. "Fruits" doesn't have name if it should be key value pair or it should be wrapped in [] not {}.

    JSON obj should be formatted like this:

    {
      "Basket" : {
        "Count": 1,
        "Fruits": [
          {
            "Name":"Mango", 
            "Season":"Summer"
          },
          {
            "Name":"Fig", 
            "Season":"Winter"
          }
        ]
      }
    }
    

    And actually invalid json shouldn't work https://play.golang.org/p/yoW7t4NfI7

    打赏 评论
  • dsfg3241 2017-02-02 01:04

    I would make 'Baskets' a struct within 'Response', create a 'BasketsData' struct, and give it all some labels.

    type Fruit struct {
        Name   string `json:"Name"`
        Season string `json:"Season"`
    }
    
    type BasketData struct {
        Count  int     `json:"Count"`
        Fruits []Fruit `json:"Fruits"`
    }
    
    type Response struct {
        Basket BasketData `json:"Basket"`
    }
    

    This way you will get a top level JSON response when you marshall it.

    fruitmania := []Fruit{{Name: "Mango", Season: "Summer"},
                           {Name: "Fig", Season: "Winter"}}
    basket := Response{BasketData{Count: 100, Fruits: fruitmania}}
    
    b, _ := json.Marshal(basket)
    fmt.Println(string(b))
    

    checkit-checkit out: https://play.golang.org/p/TuUwBLs_Ql

    打赏 评论

相关推荐 更多相似问题