golang 解析json字串(Unmarshal() 函数,解析http)

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}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄德公笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值