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 win2012 iscsi ipsec
  • ¥15 封装的 matplotlib animation 不显示图像
  • ¥15 python摄像头画面无法显示
  • ¥15 关于#3d#的问题:d标定算法(语言-python)
  • ¥15 cve,cnnvd漏洞扫描工具推荐
  • ¥15 图像超分real-esrgan网络自己训练模型遇到问题
  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)