我使用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)
}
答案 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()
}
}