dtm41506 2018-10-16 23:27
浏览 84
已采纳

从文件Golang加载Json时没有值

I hope someone could help me with this issue because I have been scratching my head for a while.

I have a project where I am trying to load json into a struct in go. I have followed exactly several tutorials online, but keep getting no data back and no error.

My json file is called page_data.json and looks like:

[
    {
        "page_title": "Page1",
        "page_description": "Introduction",
        "link": "example_link",
        "authors":
        [
            "Author1",
            "Author2",
            "Author3",
        ]
    },
    // second object, same as the first
]

But when I try the following in go:

package main

import (
"fmt"
"encoding/json"
"os"
"io/ioutil"
 )

type PageData struct {
  Title string `json: "page_title"`
  Description string `json: "page_description"`
  Link string `json: "link"`
  Authors []string `json: "authors"`
}

func main() {
    var numPages int = LoadPageData("page_data.json")
    fmt.Printf("Num Pages: %d", numPages)
}

func LoadPageData(path string) int {
    jsonFile, err := os.Open(path)
    if err != nil {
        fmt.Println(err)
    }
    defer jsonFile.Close()
    byteValue, _ := ioutil.ReadAll(jsonFile)

    var pageList []PageData

    json.Unmarshal(byteValue, &pageList)
    return len(pageList)
}

the output I get is:

Num Pages: 0

  • 写回答

1条回答 默认 最新

  • duangai2831 2018-10-17 01:40
    关注

    Fix the JSON commas and the Go struct field tags. For example,

    package main
    
    import (
        "encoding/json"
        "fmt"
        "io/ioutil"
        "os"
    )
    
    type PageData struct {
        Title       string   `json:"page_title"`
        Description string   `json:"page_description"`
        Link        string   `json:"link"`
        Authors     []string `json:"authors"`
    }
    
    func main() {
        var numPages int = LoadPageData("page_data.json")
        fmt.Printf("Num Pages: %d
    ", numPages)
    }
    
    func LoadPageData(path string) int {
        jsonFile, err := os.Open(path)
        if err != nil {
            fmt.Println(err)
        }
        defer jsonFile.Close()
        byteValue, err := ioutil.ReadAll(jsonFile)
        if err != nil {
            fmt.Println(err)
        }
        var pageList []PageData
    
        err = json.Unmarshal(byteValue, &pageList)
        if err != nil {
            fmt.Println(err)
        }
    
        fmt.Println(pageList)
    
        return len(pageList)
    }
    

    Output:

    [{Page1 Introduction example_link [Author1 Author2 Author3]}]
    

    page_data.json:

    [
        {
            "page_title": "Page1",
            "page_description": "Introduction",
            "link": "example_link",
            "authors":
            [
                "Author1",
                "Author2",
                "Author3"
            ]
        }
    ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作