golang 解析json字串

golang 解析json字串,第1张

文章目录 1. json字串解析1.1 示例 11.2. 示例2 2. go 解析 http 传递的json数据

1. json字串解析 1.1 示例 1 要解析的json格式如下
{
	"project_members": [{
			"name": "ZhaoYun",
			"number": "000005"
		},
		{
			"name": "WeiYan",
			"number": "000019"
		}
	]
}

json分析:

project_members 的值是一个组,组中有两个成员每个成员是一个map每个map有两个键值对
根据 json 创建结构体
type projectUser struct {
    Name string `json:"name"`
    Number   string `json:"number"`
}

type projectUsers struct {
    ProjectMembers []projectUser `json:"project_members"`
}

注意:json中每个key都必须在结构体中找到对应的成员。

完整代码
package main

import (
	"encoding/json"
	"fmt"
)

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

type projectUsers struct {
	ProjectMembers []projectUser `json:"project_members"`
}

func main() {
	var p projectUsers
	str := `
{
	"project_members": [{
			"name": "ZhaoYun",
			"number": "000005"
		},
		{
			"name": "WeiYan",
			"number": "000019"
		}
	]
}
`

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

}
输出如下
{[{ZhaoYun 000005} {WeiYan 000019}]}
ZhaoYun
1.2. 示例2 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}
2. go 解析 http 传递的json数据 http 的js文件

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

完整代码
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}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/993852.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存