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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gogf/gf/encoding/gparser"
     7  	"github.com/gogf/gf/os/glog"
     8  )
     9  
    10  func getWithPattern1() {
    11  	data :=
    12  		`{
    13              "users" : {
    14                      "count" : 100,
    15                      "list"  : [
    16                          {"name" : "Ming", "score" : 60},
    17                          {"name" : "John", "score" : 99.5}
    18                      ]
    19              }
    20          }`
    21  
    22  	if p, e := gparser.LoadContent([]byte(data)); e != nil {
    23  		glog.Error(e)
    24  	} else {
    25  		fmt.Println("John Score:", p.GetFloat32("users.list.1.score"))
    26  	}
    27  }
    28  
    29  func getWithPattern2() {
    30  	data :=
    31  		`<?xml version="1.0" encoding="UTF-8"?>
    32           <note>
    33             <to>Tove</to>
    34             <from>Jani</from>
    35             <heading>Reminder</heading>
    36             <body>Don't forget me this weekend!</body>
    37           </note>`
    38  
    39  	if p, e := gparser.LoadContent([]byte(data)); e != nil {
    40  		glog.Error(e)
    41  	} else {
    42  		fmt.Println("Heading:", p.GetString("note.heading"))
    43  	}
    44  }
    45  
    46  // 当键名存在"."号时,检索优先级:键名->层级,因此不会引起歧义
    47  func multiDots1() {
    48  	data :=
    49  		`{
    50              "users" : {
    51                  "count" : 100
    52              },
    53              "users.count" : 101
    54          }`
    55  	if p, e := gparser.LoadContent([]byte(data)); e != nil {
    56  		glog.Error(e)
    57  	} else {
    58  		fmt.Println("Users Count:", p.Get("users.count"))
    59  	}
    60  }
    61  
    62  func multiDots2() {
    63  	data :=
    64  		`{
    65              "users" : {
    66                  "count" : {
    67                      "type1" : 1,
    68                      "type2" : 2
    69                  },
    70                  "count.type1" : 100
    71              }
    72          }`
    73  	if p, e := gparser.LoadContent([]byte(data)); e != nil {
    74  		glog.Error(e)
    75  	} else {
    76  		fmt.Println("Users Count:", p.Get("users.count.type1"))
    77  		fmt.Println("Users Count:", p.Get("users.count.type2"))
    78  	}
    79  }
    80  
    81  // 设置数据
    82  func set1() {
    83  	data :=
    84  		`<?xml version="1.0" encoding="UTF-8"?>
    85           <article>
    86             <count>10</count>
    87             <list><title>gf article1</title><content>gf content1</content></list>
    88             <list><title>gf article2</title><content>gf content2</content></list>
    89             <list><title>gf article3</title><content>gf content3</content></list>
    90           </article>`
    91  	if p, e := gparser.LoadContent([]byte(data)); e != nil {
    92  		glog.Error(e)
    93  	} else {
    94  		p.Set("article.list.0", nil)
    95  		c, _ := p.ToJson()
    96  		fmt.Println(string(c))
    97  		// {"article":{"count":"10","list":[{"content":"gf content2","title":"gf article2"},{"content":"gf content3","title":"gf article3"}]}}
    98  	}
    99  }
   100  
   101  func set2() {
   102  	data :=
   103  		`{
   104              "users" : {
   105                  "count" : 100
   106              }
   107          }`
   108  	if p, e := gparser.LoadContent([]byte(data)); e != nil {
   109  		glog.Error(e)
   110  	} else {
   111  		p.Set("users.count", 1)
   112  		p.Set("users.list", []string{"John", "小明"})
   113  		c, _ := p.ToJson()
   114  		fmt.Println(string(c))
   115  	}
   116  }
   117  
   118  func makeXml1() {
   119  	p := gparser.New(nil)
   120  	p.Set("name", "john")
   121  	p.Set("age", 18)
   122  	p.Set("scores", map[string]int{
   123  		"语文": 100,
   124  		"数学": 100,
   125  		"英语": 100,
   126  	})
   127  	c, _ := p.ToXmlIndent("simple-xml")
   128  	fmt.Println(string(c))
   129  }
   130  
   131  func makeJson1() {
   132  	type Order struct {
   133  		Id    int     `json:"id"`
   134  		Price float32 `json:"price"`
   135  	}
   136  	p := gparser.New(nil)
   137  	p.Set("orders.list.0", Order{1, 100})
   138  	p.Set("orders.list.1", Order{2, 666})
   139  	p.Set("orders.list.2", Order{3, 999.99})
   140  	fmt.Println("Order 1 Price:", p.Get("orders.list.1.price"))
   141  	c, _ := p.ToJson()
   142  	fmt.Println(string(c))
   143  }
   144  
   145  func makeJson2() {
   146  	p := gparser.New(map[string]string{
   147  		"k1": "v1",
   148  		"k2": "v2",
   149  	})
   150  	p.Set("k1.1", []int{1, 2, 3})
   151  	//p.Set("0.0.1", []int{1,2,3})
   152  	c, _ := p.ToJson()
   153  	fmt.Println(string(c))
   154  }
   155  
   156  func makeJson3() {
   157  	p := gparser.New([]string{"a"})
   158  	p.Set("0.0.0", []int{1, 2, 3})
   159  	c, _ := p.ToJson()
   160  	fmt.Println(string(c))
   161  }
   162  
   163  func toStruct1() {
   164  	type Info struct {
   165  		Name string
   166  		Url  string
   167  	}
   168  	o := Info{}
   169  	p := gparser.New(map[string]string{
   170  		"Name": "gf",
   171  		"Url":  "https://gitee.com/johng",
   172  	})
   173  	p.ToStruct(&o)
   174  	fmt.Println("Name:", o.Name)
   175  	fmt.Println("Url :", o.Url)
   176  }
   177  
   178  func convert() {
   179  	p := gparser.New(map[string]string{
   180  		"name": "gf",
   181  		"site": "https://gitee.com/johng",
   182  	})
   183  	c, _ := p.ToJson()
   184  	fmt.Println("JSON:")
   185  	fmt.Println(string(c))
   186  	fmt.Println("======================")
   187  
   188  	fmt.Println("XML:")
   189  	c, _ = p.ToXmlIndent()
   190  	fmt.Println(string(c))
   191  	fmt.Println("======================")
   192  
   193  	fmt.Println("YAML:")
   194  	c, _ = p.ToYaml()
   195  	fmt.Println(string(c))
   196  	fmt.Println("======================")
   197  
   198  	fmt.Println("TOML:")
   199  	c, _ = p.ToToml()
   200  	fmt.Println(string(c))
   201  
   202  }
   203  
   204  func remove1() {
   205  	p := gparser.New(map[string]string{
   206  		"k1": "v1",
   207  		"k2": "v2",
   208  	})
   209  	p.Set("0.0.0.0.0.0.0.0", []int{1, 2, 3})
   210  	p.Remove("0.0")
   211  	c, _ := p.ToJson()
   212  	fmt.Println(string(c))
   213  }
   214  
   215  func main() {
   216  	toStruct1()
   217  }