zoukankan      html  css  js  c++  java
  • 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)
    }

    简单用法

  • 相关阅读:
    SpringMVC整合redis(Spring Data Redis)
    maven——pom.xml
    腾讯云Nginx配置HTTPS
    LNMP运行环境搭建
    Mac——homebrew安装PHP环境
    Yii2之路——安装配置
    Linux之路——FFmpeg安装
    PHP之路——geohash查找附近的人
    PHPStorm对laravel代码自动提示
    shell命令总结
  • 原文地址:https://www.cnblogs.com/xuweiqiang/p/14083382.html
Copyright © 2011-2022 走看看