golang 解析json字串

@

1. Unmarshal() 函数

func Unmarshal(data []byte, v interface{}) error

说明:

  • data: 要解析的字串
  • v:接收json字串解析结果的实例,可以是struct、map等等。

2. 示例一(结构体接收json字串解析结果)

  • json文件如下
{
    "project": {
        "project_name": "BeiFa",
        "project_num": "A1234567",
        "project_manger": {
            "name": "ZhuGeLiang",
            "number": "000004"
        },
        "project_members": [{
            "name": "ZhaoYun",
            "number": "000008"
        }, {
            "name": "WeiYan",
            "number": "000019"
        }]
    }
}
  • 根据json创建结构体
type projectUser struct {
    Name string `json:"name"`
    Number   string `json:"number"`
}

type project struct {
    ProjectName string `json:"project_name"`
    ProjectNum string `json:"project_num"`
    ProjectManger projectUser `json:"project_manger"`
    ProjectMembers []projectUser `json:"project_members"`
}

type projects struct {
    Project project `json:"project"`
}
  • 完整代码
package main

import (
	"encoding/json"
	"fmt"
)

type projectUser struct {
	Name string `json:"name"`
	Number   string `json:"number"`
}

type project struct {
	ProjectName string `json:"project_name"`
	ProjectNum string `json:"project_num"`
	ProjectManger projectUser `json:"project_manger"`
	ProjectMembers []projectUser `json:"project_members"`
}

type projects struct {
	Project project `json:"project"`
}

func main() {
	var p projects
	str := `
{
    "project": {
        "project_name": "BeiFa",
        "project_num": "A1234567",
        "project_manger": {
            "name": "ZhuGeLiang",
            "number": "000004"
        },
        "project_members": [{
            "name": "ZhaoYun",
            "number": "000008"
        }, {
            "name": "WeiYan",
            "number": "000019"
        }]
    }
}
`

	err := json.Unmarshal([]byte(str), &p)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(p)
	fmt.Println(p.Project.ProjectName)
	fmt.Println(p.Project.ProjectMembers[0])

}
  • 输出
{{BeiFa A1234567 {ZhuGeLiang 000004} [{ZhaoYun 000008} {WeiYan 000019}]}}
BeiFa
{ZhaoYun 000008}

3.示例二(解析从Get接口得到的json字串)

  • http 的js文件

在 nginx里写一个js文件,内容就用前边的,web访问结果如下:

image.png

  • 完整代码
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

type projectUser struct {
	Name string `json:"name"`
	Number   string `json:"number"`
}

type project struct {
	ProjectName string `json:"project_name"`
	ProjectNum string `json:"project_num"`
	ProjectManger projectUser `json:"project_manger"`
	ProjectMembers []projectUser `json:"project_members"`
}

type projects struct {
	Project project `json:"project"`
}

func main() {
	resp, err := http.Get("http://127.0.0.1/index.js")
	if err != nil {
		fmt.Println(err)
		return
	}

	body, _ := ioutil.ReadAll(resp.Body)
	//fmt.Println(string(body))
	
	var projects projects
	
	_ = json.Unmarshal(body,&projects)
	fmt.Printf("%+v", projects)
	fmt.Println(projects.Project.ProjectManger)
}
  • 输出
{Project:{ProjectName:BeiFa ProjectNum:A1234567 ProjectManger:{Name:ZhuGeLiang Number:000004} ProjectMembers:[{Name:ZhaoYun Number:000008} {Name:WeiYan Number:000019}]}}
{ZhuGeLiang 000004}

posted on 2022-02-22 20:33  运维开发玄德公  阅读(31)  评论(0编辑  收藏  举报  来源

导航