github.com/gogf/gf@v1.16.9/.example/encoding/gjson/issue283.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/gogf/gf/encoding/gjson"
     5  	"github.com/gogf/gf/frame/g"
     6  	"github.com/gogf/gf/os/glog"
     7  )
     8  
     9  type GameUser struct {
    10  	Uid        int         `json:"uid"`
    11  	Account    string      `json:"account"`
    12  	Tel        string      `json:"tel"`
    13  	Role       string      `json:"role"`
    14  	Vip        int         `json:"vip"`
    15  	GameLevel  int         `json:"gamelevel"`
    16  	Diamond    int         `json:"diamond"`
    17  	Coin       int         `json:"coin"`
    18  	Value      int         `json:"value"`
    19  	Area       string      `json:"area"`
    20  	ServerName string      `json:"servername"`
    21  	Time       int         `json:"time"`
    22  	ClientInfo *ClientInfo `json:"client_info"`
    23  }
    24  
    25  type ClientInfo struct {
    26  	ClientGuid       string `json:"client_guid"`
    27  	ClientType       int    `json:"client_type"`
    28  	ClientSDKVersion string `json:"client_sdk_version"`
    29  	ClientVersion    string `json:"client_version"`
    30  	PackageId        string `json:"packageid"`
    31  	PhoneType        string `json:"phone_type"`
    32  	DevicesId        string `json:"devices_id"`
    33  	ClientMac        string `json:"client_mac"`
    34  }
    35  
    36  func main() {
    37  	s := `{
    38      "uid":9527,          			    
    39      "account":"zhangsan",          	    
    40      "tel":"15248787",          	     
    41      "role":"test",          		   
    42      "vip":7,          		   			
    43      "gamelevel":59,          		  
    44      "diamond ":59,          		   
    45      "coin ":59,          		      
    46      "value ":99,          		   
    47      "area":"s",          		       
    48      "servername":"灵动",          	
    49      "time":15454878787,          	
    50      "client_info": {
    51  		"client_guid": "aaaa", 
    52  		"client_type": 1,      
    53  		"client_sdk_version": "1.0.1",  
    54  		"client_version": "1.0.1",     
    55  		"packageid":"",                 
    56  		"phone_type": "vivi",
    57  		"devices_id":"",   
    58  		"client_mac":""      
    59      }
    60  }`
    61  
    62  	gameUser := &GameUser{}
    63  	err := gjson.DecodeTo(s, gameUser)
    64  	if err != nil {
    65  		glog.Error(err)
    66  	}
    67  	g.Dump(gameUser)
    68  
    69  }