普通网友 2014-02-20 00:38
浏览 104
已采纳

将嵌套的JSON响应解组到嵌套的struct中

Here's the sample JSON returning from the web API I'm invoking:

{
    "Ability1": "Noxious Fumes",
    "AbilityId1": 7812,
    "AttackSpeed": 0.86,
    "Cons": "",
    "HP5PerLevel": 0.47,
    "Health": 360,
    "Speed": 350,
    "abilityDescription1": {
      "itemDescription": {
        "cooldown": "12s",
        "cost": "60/70/80/90/100",
        "description": "Agni summons a cloud of noxious fumes at his ground target location, doing damage every second. Firing any of Agni's abilities into the fumes detonates the gas, stunning all enemies in the radius.",
        "menuitems": [
          {
            "description": "Ability:",
            "value": "Ground Target"
          },
          {
            "description": "Affects:",
            "value": "Enemy"
          },
          {
            "description": "Damage:",
            "value": "Magical"
          },
          {
            "description": "Radius:",
            "value": "20"
          }
        ],
        "rankitems": [
          {
            "description": "Damage per Tick:",
            "value": "10/20/30/40/50 (+5% of your magical power)"
          },
          {
            "description": "Fumes Duration:",
            "value": "10s"
          },
          {
            "description": "Stun Duration:",
            "value": "1s"
          }
        ],
        "secondaryDescription": ""
      }
    },
    "abilityDescription5": {
      "itemDescription": {
        "cooldown": "",
        "cost": "",
        "description": "After hitting with 4 basic attacks, Agni will gain a buff. On the next cast of Flame Wave or Rain Fire, all enemies hit by those abilities will be additionally set ablaze, taking damage every .5s for 3s.",
        "menuitems": [
          {
            "description": "Affects:",
            "value": "Enemy"
          },
          {
            "description": "Damage:",
            "value": "Magical"
          }
        ],
        "rankitems": [
          {
            "description": "Damage per Tick:",
            "value": "5 (+10% of your magical power)"
          }
        ],
        "secondaryDescription": ""
      }
    },
    "basicAttack": {
      "itemDescription": {
        "cooldown": "",
        "cost": "",
        "description": "",
        "menuitems": [
          {
            "description": "Damage:",
            "value": "34 + 1.5/Lvl (+20% of Magical Power)"
          },
          {
            "description": "Progression:",
            "value": "None"
          }
        ],
        "rankitems": [],
        "secondaryDescription": ""
      }
    },
    "id": 1737,
    "ret_msg": null
  }

And my struct:

type God struct {
    Ability1                      string
    Ability2                      string
    Ability3                      string
    Ability4                      string
    Ability5                      string
    AbilityId1                    int
    AbilityId2                    int
    AbilityId3                    int
    AbilityId4                    int
    AbilityId5                    int
    Attack_speed                  float64
    Attack_speed_per_level        float64
    Cons                          string
    Hp5_per_level                 float64
    Health                        int
    Health_per_five               int
    Health_per_level              int
    Item1                         string
    Item2                         string
    Item3                         string
    Item4                         string
    Item5                         string
    Item6                         string
    Item7                         string
    Item8                         string
    Item9                         string
    ItemId1                       int
    ItemId2                       int
    ItemId3                       int
    ItemId4                       int
    ItemId5                       int
    ItemId6                       int
    ItemId7                       int
    ItemId8                       int
    ItemId9                       int
    Lore                          string
    Mp5_per_level                 float64
    Magic_protection              int
    Magic_protection_per_level    int
    Mana                          int
    Mana_per_five                 float64
    Mana_per_level                int
    Name                          string
    On_free_rotation              string
    Pantheon                      string
    Physical_power                int
    Physical_power_per_level      int
    Physical_protection           int
    Physical_protection_per_level float64
    Pros                          string
    Roles                         string
    Speed                         int
    Title                         string
    Type                          string
    Abilitydescription1           struct {
        Item_description struct {
            Cooldown    string
            Cost        string
            Description string
            Menu_items  struct {
                Description string
                Value       string
            }
            Rank_items struct {
                Description string
                Value       string
            }
            Secondary_description string
        }
    }
    Ability_description2 struct {
        Item_description struct {
            Cooldown    string
            Cost        string
            Description string
            Menu_items  struct {
                Description string
                Value       string
            }
            Rank_items struct {
                Description string
                Value       string
            }
            Secondary_description string
        }
    }
    Ability_description3 struct {
        Item_description struct {
            Cooldown    string
            Cost        string
            Description string
            Menu_items  struct {
                Description string
                Value       string
            }
            Rank_items struct {
                Description string
                Value       string
            }
            Secondary_description string
        }
    }
    Ability_description4 struct {
        Item_description struct {
            Cooldown    string
            Cost        string
            Description string
            Menu_items  struct {
                Description string
                Value       string
            }
            Rank_items struct {
                Description string
                Value       string
            }
            Secondary_description string
        }
    }
    Ability_description5 struct {
        Item_description struct {
            Cooldown    string
            Cost        string
            Description string
            Menu_items  struct {
                Description string
                Value       string
            }
            Rank_items struct {
                Description string
                Value       string
            }
            Secondary_description string
        }
    }
    Basic_attack struct {
        Item_description struct {
            cooldown    string
            cost        string
            description string
            Menu_items  struct {
                Description string
                Value       string
            }
            Rank_items struct {
                Description string
                Value       string
            }
            Secondary_description string
        }
    }
    Id      int
    Ret_msg string
}

Here's how I unmarshal the JSON response into the struct array:

var gods []God
json.Unmarshal(jsonResponse, &gods)
return gods

Everything is marshalling properly except the abilityDescription1(2,3,4,5) and everything within that struct.

Any suggestions?

  • 写回答

1条回答 默认 最新

  • dongmei425373 2014-02-20 22:16
    关注

    Use struct tags to map JSON field names to struct field names.

    Many of your struct fields are lowercase and therefore unexported, which means the encoding/json package cannot access/marshal into them. Take a look at http://golang.org/pkg/reflect/#StructTag (the JSON package will match "Description" with "description", though - i.e. lower-case is already a match)

    You are also better off (for readability/ability to add tags) splitting your structs into separate items, and then embedding them. You have cases where you're repeating Rank_items, whereas you could define it once and then embed it as needed:

    e.g.

       Basic_attack struct {
            ItemDescription ItemDesc `json:"itemDescription"`
            Menu []MenuItems `json:"menuitems"`
            Rank []RankItems  `json:"rankitems"`
       }
    
       ItemDesc struct {
            Cooldown    string `json:"cooldown"`
            Cost        string `json:"cost"`
            Description string `json:"description"`
            Menu        []MenuItems `json:"menuitems"`
            ...
       }
    
       MenuItems  struct {
                Description string 
                Value       string
      }
    
       RankItems struct {
                    Description string
                    Value       string
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题