• Golang将json字符串转json对象


    从goframe里面拆一些工具用

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main() {
        type Response struct {
            RequestID     string                   `json:"RequestId"`
            SendStatusSet []map[string]interface{} `json:"SendStatusSet"`
        }
        type r struct {
            Response Response `json:"Response"`
        }
        txt := `{
            "Response": {
                "SendStatusSet": [{
                        "SerialNo": "5000:1045710669157053657849499619",
                        "PhoneNumber": "+8618511122233",
                        "Fee": 1,
                        "SessionContext": "test",
                        "Code": "Ok",
                        "Message": "send success",
                        "IsoCode": "CN"
                    },
                    {
                        "SerialNo": "5000:104571066915705365784949619",
                        "PhoneNumber": "+8618511122266",
                        "Fee": 1,
                        "SessionContext": "test",
                        "Code": "Ok",
                        "Message": "send success",
                        "IsoCode": "CN"
                    }
                ],
                "RequestId": "a0aabda6-cf91-4f3e-a81f-9198114a2279"
            }
        }`
        // fmt.Println(txt)
        p := &r{}
        err := json.Unmarshal([]byte(txt), p)
        fmt.Println(err)
        fmt.Println(*p)
    
        type simple struct {
            Response map[string]interface{}
        }
        zzz := new(simple)
        err = json.Unmarshal([]byte(txt), zzz)
        fmt.Println(err)
        fmt.Println("--------------")
        fmt.Println(*zzz)
    
        simpleJSON := `{"Name":"Xiao mi 6","ProductID":1,"Number":10000,"Price":2499,"IsOnSale":true}`
        type k struct {
            Name string
        }
        kk := &k{}
        err = json.Unmarshal([]byte(simpleJSON), kk)
        fmt.Println(err)
        fmt.Println(*kk)
    }

    简单用法

  • 相关阅读:
    td-agent 收集日志到kafka的配置
    ctrl+z 以后怎么恢复挂起的进程
    LCD显示GPS时钟[嵌入式系统]
    树莓派轮盘游戏机[嵌入式系统]
    集成测试工具
    未上线的界面
    前端网页内复杂编辑
    用jquery编写的分页插件
    用jquery编写的tab插件
    EAA脚本语言0.2
  • 原文地址:https://www.cnblogs.com/xuweiqiang/p/14083382.html
Copyright © 2020-2023  润新知