File: README.md

package info (click to toggle)
golang-github-likexian-gokit 0.24.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 720 kB
  • sloc: makefile: 5
file content (99 lines) | stat: -rw-r--r-- 1,958 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# GoKit - xjson

Json kits for Golang development.

## Features

- Easy load to json and dump to string
- Load and dump with file is supported
- Modify the json data is simple
- One line retrieval with MustXXX
- Get by dot notation key is supported

## Installation

    go get -u github.com/likexian/gokit

## Importing

    import (
        "github.com/likexian/gokit/xjson"
    )

## Documentation

Visit the docs on [GoDoc](https://godoc.org/github.com/likexian/gokit/xjson)

## Example

### Dump the struct data to JSON string

```go
// Define Status struct
type Status struct {
    Code    int64  `json:"code"`
    Message string `json:"message"`
}

// Init status
status := Status{1, "Success"}

// Dump status to json string
j := xjson.New(status)
s, err := j.Dumps()
if err == nil {
    fmt.Println("Json text is:", s)
}

// OR dumps using the easy way
s, err := xjson.Dumps(status)
if err == nil {
    fmt.Println("Json text is:", s)
}
```

### Dump the map data to JSON string

```go
// Init a map data
data := map[string]interface{}{
    "code": 1,
    "message": "success",
    "result": {
        "Name": "Li Kexian"
    }
}

// Dump to string in the easy way
s, err := xjson.Dumps(status)
if err == nil {
    fmt.Println("Json text is:", s)
}
```

### Load the JSON string

```go
// Json strig
text := `{"Code": 1, "Message": "Success", "Result": {"Student": [{"Name": "Li Kexian"}]}}`

// Load json string
j, err := xjson.Loads(text)
if err == nil {
    fmt.Println("Code is:", j.Get("Code").MustInt(0))
    fmt.Println("Message is:", j.Get("Message").MustString(""))
    fmt.Println("First Student name is:", j.Get("Result.Student.0.Name").MustString("-"))
}
```

## License

Copyright 2012-2021 [Li Kexian](https://www.likexian.com/)

Licensed under the Apache License 2.0

## Donation

If this project is helpful, please share it with friends.

If you want to thank me, you can [give me a cup of coffee](https://www.likexian.com/donate/).