golang json unmarshal #golang
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang json unmarshal #golang相关的知识,希望对你有一定的参考价值。
type MergeRequests struct {
Title string `json:"title"`
}
func main() {
resp, _ := http.Get("https://gitlab.com/api/v4/projects/4600432/merge_requests?private_token=")
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var mrs []MergeRequests
json.Unmarshal(body, &mrs)
fmt.Printf("%v", mrs[0].Title)
}
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Project struct {
Name string `json:"name_with_namespace"`
Links map[string]string `json:"_links"`
}
type Resp struct {
Projects []Project `json:"projects"`
}
type Merge struct {
Merge_requests string `json:"merge_requests"`
Self string `json:"self"`
}
func main() {
resp, _ := http.Get("https://gitlab.com/api/v4/groups/1768437?private_token=")
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
res := Resp{}
json.Unmarshal(body, &res)
fmt.Printf("%v", res.Projects[0].Links["merge_requests"])
}
//read file
func main() {
file, e := ioutil.ReadFile("./config.json")
if e != nil {
fmt.Printf("File error: %v\n", e)
os.Exit(1)
}
fmt.Printf("%s\n", string(file))
//m := new(Dispatch)
//var m interface{}
var jsontype jsonobject
json.Unmarshal(file, &jsontype)
fmt.Printf("Results: %v\n", jsontype)
}
// best struct
var data = `
a: Easy!
b:
c: 2
d: [3, 4]
`
// Note: struct fields must be public in order for unmarshal to
// correctly populate the data.
type T struct {
A string
B struct {
RenamedC int `yaml:"c"`
D []int `yaml:",flow"`
}
}
golang json_unmarshal
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Project struct {
Name string `json:"name_with_namespace"`
Links map[string]string `json:"_links"`
}
type Resp struct {
Projects []Project `json:"projects"`
}
type Merge struct {
Merge_requests string `json:"merge_requests"`
Self string `json:"self"`
}
func main() {
resp, _ := http.Get("https://gitlab.com/api/v4/groups/1768437?private_token=ra4HxoNg1adKgVyYURhu")
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
res := Resp{}
json.Unmarshal(body, &res)
fmt.Printf("%v", res.Projects[0].Links["merge_requests"])
}
以上是关于golang json unmarshal #golang的主要内容,如果未能解决你的问题,请参考以下文章
go json.Unmarshal报错invalid character ' ï' looking for beginning of value
golang json unmarshal #golang
golang json_unmarshal
Go_14:GoLang中 jsonmapstruct 之间的相互转化
Golang json Unmarshal “JSON 输入意外结束”
Golang解析json的特殊情况处理