dongposhi8677 2013-06-05 04:20
浏览 58
已采纳

在Go语言中,如何将json解组到对象数组?

I have the following JSON, and I want to parse it into array of class:

{
    "1001": {"level":10, "monster-id": 1001, "skill-level": 1, "aimer-id": 301}
    "1002": {"level":12, "monster-id": 1002, "skill-level": 1, "aimer-id": 302}
    "1003": {"level":16, "monster-id": 1003, "skill-level": 2, "aimer-id": 303}
}

Here is what i am trying to do but failed:

type Monster struct {
    MonsterId  int32
    Level      int32
    SkillLevel int32
    AimerId    int32
}


type MonsterCollection struct {
    Pool map[string]Monster
}

func (mc *MonsterCollection) FromJson(jsonStr string) {
    var data interface{}
    b := []byte(jsonStr)
    err := json.Unmarshal(b, &data)
    if err != nil {
        return
    }

    m := data.(map[string]interface{})

    i := 0
    for k, v := range m {

        monster := new(Monster)
        monster.Level = v["level"]
        monster.MonsterId = v["monster-id"]
        monster.SkillLevel = v["skill-level"]
        monster.AimerId = v["aimer-id"]

        mc.Pool[i] = monster
        i++
    }

}

The compiler complain about the v["level"] << invalid operation. index of type interface().

  • 写回答

2条回答 默认 最新

  • douji9734 2013-06-05 04:58
    关注

    This code has many errors in it. To start with, the json isn't valid json. You are missing the commas in between key pairs in your top level object. I added the commas and pretty printed it for you:

    {
       "1001":{
          "level":10,
          "monster-id":1001,
          "skill-level":1,
          "aimer-id":301
       },
       "1002":{
          "level":12,
          "monster-id":1002,
          "skill-level":1,
          "aimer-id":302
       },
       "1003":{
          "level":16,
          "monster-id":1003,
          "skill-level":2,
          "aimer-id":303
       }
    }
    

    Your next problem (the one you asked about) is that m := data.(map[string]interface{}) makes m a map[string]interface{}. That means when you index it such as the v in your range loop, the type is interface{}. You need to type assert it again with v.(map[string]interface{}) and then type assert each time you read from the map.


    I also notice that you next attempt mc.Pool[i] = monster when i is an int and mc.Pool is a map[string]Monster. An int is not a valid key for that map.


    Your data looks very rigid so I would make unmarshall do most of the work for you. Instead of providing it a map[string]interface{}, you can provide it a map[string]Monster.

    Here is a quick example. As well as changing how the unmarshalling works, I also added an error return. The error return is useful for finding bugs. That error return is what told me you had invalid json.

    type Monster struct {
        MonsterId  int32 `json:"monster-id"`
        Level      int32 `json:"level"`
        SkillLevel int32 `json:"skill-level"`
        AimerId    int32 `json:"aimer-id"`
    }
    
    type MonsterCollection struct {
        Pool map[string]Monster
    }
    
    func (mc *MonsterCollection) FromJson(jsonStr string) error {
        var data = &mc.Pool
        b := []byte(jsonStr)
        return json.Unmarshal(b, data)
    }
    

    I posted a working example to goplay: http://play.golang.org/p/4EaasS2VLL

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)