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 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?