dongnai8013 2017-08-24 03:19
浏览 195
已采纳

如何解析Json内部的部分对象

I have the Json structure below, and i'm trying to parse only the key field inside the object. It's possible to do it without mapping the complete structure?

{
 "Records":[
  {
     "eventVersion":"2.0",
     "s3":{
        "s3SchemaVersion":"1.0",
        "configurationId":"my-event",
        "bucket":{
           "name":"super-files",
           "ownerIdentity":{
              "principalId":"41123123"
           },
           "arn":"arn:aws:s3:::myqueue"
        },
        "object":{
           "key":"/events/mykey",
           "size":502,
           "eTag":"091820398091823",
           "sequencer":"1123123"
         }
       }
    }
  ]
}

// Want to return only the Key value
type Object struct {
   Key string `json:"key"`
}
  • 写回答

2条回答 默认 最新

  • doukun1450 2017-08-24 03:36
    关注

    Object is part of S3, so I created struct as below and I was able to read key

    type Root struct {
        Records []Record `json:"Records"`
    }
    
    
    type Record struct {
        S3 SS3 `json:"s3"`
    }
    
    type SS3 struct {
        Obj Object `json:"object"`
    }
    
    type Object struct {
        Key string `json:"key"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?