转换表单json字符串后如何打印数组

时间:2019-07-13 02:14:55

标签: go

我使用json.Unmarshal()将json字符串转换为struct custom类型的数组。 我无法打印此数组。如何打印?

我尝试了fmt.Printf("%v")fmt.Prinlnf("%+v")的结果[]

package main

import (
    "encoding/json"
    "fmt"
)

// Job Type:
type Job struct {
    link       string
    isComplete bool
}

func main() {

    jobsJSON := `[
            {"link": "https://facebook.com", "isComplete": false},
            {"link": "https://google.com", "isComplete": false},
            {"link": "https://youtube.com", "isComplete": false},
    ]`

    var jobs []Job
    json.Unmarshal([]byte(jobsJSON), &jobs)

    fmt.Printf("%+v", jobs)

}

1 个答案:

答案 0 :(得分:0)

package main

import (
    "encoding/json"
    "fmt"
)

// Job Type:
type Job struct {
    Link       string
    isComplete bool
}

func main() {

    jobsJSON := "[{\"link\": \"https://facebook.com\", \"isComplete\": false},{\"link\": \"https://google.com\", \"isComplete\": false},{\"link\": \"https://youtube.com\", \"isComplete\": false}]"

    var jobs []Job
    json.Unmarshal([]byte(jobsJSON), &jobs)

    for i := range jobs {
        fmt.Printf("link: %v, isComplete: %v", jobs[i].Link, jobs[i].isComplete)
        fmt.Println()
    }

}