登录
首页 >  Golang >  Go问答

将 JSON 文件读入自定义类型结构,我需要在需要类型字符串而不是自定义类型的函数中使用键

来源:stackoverflow

时间:2024-04-08 16:12:36 213浏览 收藏

哈喽!今天心血来潮给大家带来了《将 JSON 文件读入自定义类型结构,我需要在需要类型字符串而不是自定义类型的函数中使用键》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我正在创建一个工具,可以获取 json 文件,然后使用 go 创建 pdf

这是我的 json 示例:

[{"name":"ollie","age":"25","comment":"this is my comment"},{"name":"amy","age":"28","comment":"another comment"},{"name":"joey","age":"19","comment":"comment from joey"},{"name":"james","age":"23","comment":"james' comment"},{"name":"richard","age":"20","comment":"richard has also made 24"}]

我有一些可以使用 csv 文件工作的东西,但现在我也希望能够获取 json 文件

我用来创建 pdf 的包是 gofpdf

问题之一是我需要将 json 传递到一个结构中才能读取它,该结构有它自己的自定义类型 - 因为我使用的是自定义类型,所以我无法将值传递到 gofpdf 的函数中制作 pdf

我只是希望能够将结构中的值(声明为字符串)作为函数中的字符串传递:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "time"

    "github.com/jung-kurt/gofpdf"
)

这是我的结构:

type person struct {
    name    string `json:"name"`
    age     string `json:"age"`
    comment string `json:"comment"`
}

func main() {

    data, err := ioutil.readfile("csv-to-json.json")
    if err != nil {
        fmt.println(err)
    }

    //create variable people which is array of person structs
    var people []person

    //take the data object (json file) and place into the people array of struct
    json.unmarshal(data, &people)

    fmt.println(people[:])

    pdf := newreport()

    pdf = createtablejson(pdf, people[:])

    if pdf.err() {
        log.fatalf("failed creating pdf report: %s\n", pdf.error())
    }

    err = savepdfjson(pdf)
    if err != nil {
        log.fatalf("cannot save pdf: %s|n", err)
    }
}

func createtablejson(pdf *gofpdf.fpdf, table []person) *gofpdf.fpdf {
    for _, str := range table {

下面是我正在挣扎的地方。 str 需要是字符串类型

pdf.cellformat(20, 7, str, "1", 0, "c", false, 0, "")

        pdf.ln(-1)
    }
    return pdf
}

//create function that generates a new pdf report
func newreport() *gofpdf.fpdf {
    pdf := gofpdf.new("p", "mm", "letter", "")
    pdf.addpage()
    pdf.setfont("arial", "b", 28)
    pdf.cell(40, 10, "my title for the pdf (new)!")
    pdf.ln(12)
    pdf.setfont("arial", "", 11)
    pdf.cell(40, 10, time.now().format("mon jan 2, 2006"))
    pdf.ln(12)

    return pdf
}


func savepdfjson(pdf *gofpdf.fpdf) error {
    return pdf.outputfileandclose("pdf_from_json.pdf")
}

因此,我能够从 json 文件读取数据并将行打印到控制台,但我无法在 pdf 生成函数中使用这些数据,因为我必须创建自定义类型,并且函数参数需要字符串。有人可以帮我从这里出去吗?我想要这样的等价物:

pdf.CellFormat(20, 7, **TOSTRING(str)**, "1", 0, "C", false, 0, "")

我已经闲逛了大约 3 个小时,但没有任何运气

提前致谢


解决方案


您需要实现 (p *person) string() string 以便获得 person 的字符串表示形式。

例如:

func (p *Person) String() string {
  return fmt.Sprintf("%s age %d says %q", p.Name, p.Age, p.Comment)
}

// ...
pdf.CellFormat(20, 7, str.String(), "1", 0, "C", false, 0, "")

好了,本文到此结束,带大家了解了《将 JSON 文件读入自定义类型结构,我需要在需要类型字符串而不是自定义类型的函数中使用键》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>