douxiong2738 2018-11-29 11:34
浏览 75
已采纳

如何在golang中解析json数组结构

im using the following code without success to parse json value but its inside array []

https://play.golang.org/p/5HdWeyEtvie

package main

import (
    "encoding/json"
    "fmt"
)


var input = `
[
{
    "created_at": "Thu May 31 00:00:01 +0000 2012"
},
{
    "created_at": "Thu May 31 00:00:01 +0000 2012"
}
]
`

func main() {
    var val map[string]interface{}
    if err := json.Unmarshal([]byte(input), &val); err != nil {
        panic(err)
    }
    fmt.Println(val)
}

The idea is to get the value as key and print it , like a function that get string parameter="created_at" and prints it.

  • 写回答

4条回答 默认 最新

  • dqp99585 2018-11-29 11:42
    关注

    You're unmarshaling an array to a map. That obviously won't work. you need val to be an array.

    func main() {
        var val []map[string]interface{} // <---- This must be an array to match input
        if err := json.Unmarshal([]byte(input), &val); err != nil {
            panic(err)
        }
        fmt.Println(val)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongsha1544 2018-11-29 11:42
    关注
    var val []struct{
        CreatedAt string `json:"created_at"`
    }{}
    
    if err := json.Unmarshal([]byte(input), &val); err != nil {
        panic(err)
    }
    
    评论
  • doubeishuai6598 2018-11-29 13:24
    关注

    You should create a struct like this.

    type Data struct {
        CreateAt time.Time `json:"create_at"`
    }
    
    func main() {
        var in []Data
        if err := json.Unmarshal([]byte(input), &in); err != nil{
            log.Fatal(err)
        }
        for _, data := range in{
            fmt.Println(data.CreateAt)
        }
    }
    
    评论
  • dsdtumf776629385 2018-12-20 14:52
    关注

    Your input is:

    [  
       {  
          "created_at":"Thu May 31 00:00:01 +0000 2012"
       },
       {  
          "created_at":"Thu May 31 00:00:01 +0000 2012"
       }
    ]
    

    You can turn this into a struct like so:

    type MyArray []struct {
        CreatedAt string `json:"created_at"`
    }
    

    Now you can read your JSON data and loop through it to get your desired values. Here's what you get:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    var str = `[
        {
        "created_at": "Thu May 31 00:00:01 +0000 2012"
        },
        {
        "created_at": "Thu May 31 00:00:01 +0000 2013"
        }
    ]`
    
    type MyArray struct {
        CreatedAt string `json:"created_at"`
    }
    
    func main() {
        var m []MyArray
        if err := json.Unmarshal([]byte(str), &m); err != nil {
            panic(err)
        }
        for _, val := range m {
            fmt.Println(val.CreatedAt)
        }
    }
    

    In the future, you could consult JSON-to-Go if you're unsure how to convert a JSON object to a struct -- it's quite useful. Cheers!

    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 用C语言绘制cs1.6方框透视 出现了点问题 绘制不上去 矩阵数据 和敌人坐标都是正确的
  • ¥15 Tpad api账户 api口令
  • ¥30 ppt进度条制作,vba语言
  • ¥15 生信simpleaffy包下载
  • ¥15 请教一下simulink中S函数相关问题
  • ¥15 在二层网络中,掩码存在包含关系即可通信
  • ¥15 端口转发器解析失败不知道电脑设置了啥
  • ¥15 Latex算法流程图行号自定义
  • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)
  • ¥40 基于51单片机实现球赛计分器功能