douqujin2767 2017-09-06 22:49
浏览 41
已采纳

在Go中解析JSON

This is an example of JSON output when calling 'ListObjects' for AWS S3

{
    "Contents": [{
        "ETag": "9e2bc2894b23742b7bb688c646c6fee9",
        "Key": "DSC-0237.jpg",
        "LastModified": "2017-09-06 21:53:15 +0000 UTC",
        "Owner": {
            "DisplayName": "demo-user",
            "ID": "a9e2f170a6880f1d61852df8e523e88ca2a2b7abd093476cc93f1239ab5063c6"
        },
        "Size": 117904,
        "StorageClass": "STANDARD"
    }, {
        "ETag": "\"9e2bc2894b23742b7bb688c646c6fee9\"",
        "Key": "DSC-0238.jpg",
        "LastModified": "2017-09-06 21:52:24 +0000 UTC",
        "Owner": {
            "DisplayName": "demo-user",
            "ID": "a9e2f170a6880f1d61852df8e523e88ca2a2b7abd093476cc93f1239ab5063c6"
        },
        "Size": 117904,
        "StorageClass": "STANDARD"
    }, {
        "ETag": "\"9e2bc2894b23742b7bb688c646c6fee9\"",
        "Key": "DSC-0239.jpg",
        "LastModified": "2017-09-06 21:53:01 +0000 UTC",
        "Owner": {
            "DisplayName": "demo-user",
            "ID": "a9e2f170a6880f1d61852df8e523e88ca2a2b7abd093476cc93f1239ab5063c6"
        },
        "Size": 117904,
        "StorageClass": "STANDARD"
    }],
    "IsTruncated": false,
    "Marker": "",
    "MaxKeys": 5,
    "Name": "test-bucket-x011pp3",
    "Prefix": ""
}

How do I parse this in Go? Chiefly I am interested in collecting:

  • Bucket Name
  • Key
  • Size
  • Owner's DisplayName
  • LastModified

I am coming from Python and in Python it would be something really simple like:

json_result = json.loads(json_string)
bucket_name = json_result['Name']
for idx, obj in enumerate(json_result['Contents']):
    key = obj['Key']
    size = obj['Size']
    lastmod = obj['LastModified']
    owner= obj['Owner']['DisplayName']

Thank you for the help!

  • 写回答

3条回答 默认 最新

  • dongyan8896 2017-09-06 23:00
    关注

    It is something like this

    package main
    
    import (
        "bytes"
        "encoding/json"
        "fmt"
        "log"
    )
    
    var jsonData = []byte(`
    {
        "Contents": [{
            "ETag": "9e2bc2894b23742b7bb688c646c6fee9",
            "Key": "DSC-0237.jpg",
            "LastModified": "2017-09-06 21:53:15 +0000 UTC",
            "Owner": {
                "DisplayName": "demo-user",
                "ID": "a9e2f170a6880f1d61852df8e523e88ca2a2b7abd093476cc93f1239ab5063c6"
            },
            "Size": 117904,
            "StorageClass": "STANDARD"
        }, {
            "ETag": "\"9e2bc2894b23742b7bb688c646c6fee9\"",
            "Key": "DSC-0238.jpg",
            "LastModified": "2017-09-06 21:52:24 +0000 UTC",
            "Owner": {
                "DisplayName": "demo-user",
                "ID": "a9e2f170a6880f1d61852df8e523e88ca2a2b7abd093476cc93f1239ab5063c6"
            },
            "Size": 117904,
            "StorageClass": "STANDARD"
        }, {
            "ETag": "\"9e2bc2894b23742b7bb688c646c6fee9\"",
            "Key": "DSC-0239.jpg",
            "LastModified": "2017-09-06 21:53:01 +0000 UTC",
            "Owner": {
                "DisplayName": "demo-user",
                "ID": "a9e2f170a6880f1d61852df8e523e88ca2a2b7abd093476cc93f1239ab5063c6"
            },
            "Size": 117904,
            "StorageClass": "STANDARD"
        }],
        "IsTruncated": false,
        "Marker": "",
        "MaxKeys": 5,
        "Name": "test-bucket-x011pp3",
        "Prefix": ""
    }`,
    )
    
    type Response struct {
        Contents    []*Content
        IsTruncated bool
        Marker      string
        MaxKeys     int
        Name        string
        Prefix      string
    }
    
    type Content struct {
        ETag         string
        Key          string
        LastModified string
        Owner        *Owner
        Size         int
        StroageClass string
    }
    
    type Owner struct {
        DisplayName string
        ID          string
    }
    
    func main() {
        resp := &Response{}
    
        if err := json.NewDecoder(bytes.NewBuffer(jsonData)).Decode(resp); err != nil {
            log.Fatal(err)
        }
    
        fmt.Printf("%#v", resp)
    }
    

    Try it in the Go Playground

    And you should read this https://golang.org/pkg/encoding/json/

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

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序