douqian5553 2016-11-21 17:35
浏览 26
已采纳

在golang中解析json文件[关闭]

I have a json structure like this

{  
"some text":[  
  {  
     "sha":"1234567",
     "message":"hello world",
     "author":"varung",
     "timestamp":1479445228
  }
]
}

I need to access the value of sha, using golang. How can I do it?

  • 写回答

2条回答 默认 最新

  • doushuichong2589 2016-11-21 17:51
    关注

    You can use Go's encoding/json package quite easily. Here is a playground link to the code below.

    package main
    
    import (
        "encoding/json"
        "fmt"
        "log"
    )
    
    var a = `{  
    "sometext":[  
      {  
         "sha":"1234567",
         "message":"hello world",
         "author":"varung",
         "timestamp":1479445228
      }
    ]
    }`
    
    type Root struct {
        Text []*Object `json:"sometext"`
    }
    
    type Object struct {
        Sha       string `json:"sha"`
        Message   string `json:"message"`
        Author    string `json:"author"`
        Timestamp int    `json:"timestamp"`
    }
    
    func main() {
        var j Root
        err := json.Unmarshal([]byte(a), &j)
        if err != nil {
            log.Fatalf("error parsing JSON: %s
    ", err.Error())
        }
        fmt.Printf("%+v
    ", j.Text[0].Sha)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制