美文网首页
golong的js字串解析

golong的js字串解析

作者: 玄德公笔记 | 来源:发表于2022-02-17 22:39 被阅读0次

    1. js解析

    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访问结果如下:

    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}
    

    相关文章

      网友评论

          本文标题:golong的js字串解析

          本文链接:https://www.haomeiwen.com/subject/jobllrtx.html