-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added golang examples marshal/unmarshal json
- Loading branch information
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"request": { | ||
"user":"Admin", | ||
"msg":"Try some other...", | ||
"tags": "main,some,shit" | ||
}, | ||
"user":"Admin2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"request": { | ||
"user":"Admin", | ||
"msg":"Try some other..." | ||
}, | ||
"user":"Admin2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"encoding/json" | ||
"strings" | ||
) | ||
|
||
type RequestContent struct { | ||
User string | ||
Message string `json:"msg"` | ||
} | ||
|
||
type Request struct { | ||
Request RequestContent | ||
Author string `json:"user"` | ||
} | ||
|
||
type strslice []string | ||
|
||
func (ss *strslice) UnmarshalJSON(data []byte) error { | ||
var s string | ||
if err := json.Unmarshal(data, &s); err != nil { | ||
return err | ||
} | ||
*ss = strings.Split(s, ",") | ||
return nil | ||
} | ||
|
||
type RequestContentTag struct { | ||
User string | ||
Message string `json:"msg"` | ||
Tags strslice `json:"tags"` | ||
} | ||
|
||
type RequestTagged struct { | ||
Request RequestContentTag | ||
Author string `json:"user"` | ||
} | ||
|
||
type Dimensions struct { | ||
Height int | ||
Width int | ||
} | ||
|
||
type Bird struct { | ||
Species string | ||
Description string | ||
Dimensions Dimensions | ||
} | ||
|
||
func ParseJson(birdJson string) { | ||
// birdJson := `{"species":"pingeon","description":"Likes to perch on rocks...","dimensions":{"height": 22,"width": 41}}` | ||
var bird Bird | ||
err := json.Unmarshal([]byte(birdJson), &bird) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(bird) | ||
} | ||
|
||
func ParseJsonArrays() { | ||
arraysJson := `["some","word","list"]` | ||
var nums []string | ||
|
||
json.Unmarshal([]byte(arraysJson), &nums) | ||
fmt.Println(nums) | ||
} | ||
|
||
func CreateJson() string { | ||
bird := Bird{ | ||
Species: "Eagle", | ||
Description: "Some eagle.", | ||
Dimensions: Dimensions{ | ||
Height: 120, | ||
Width: 60, | ||
}, | ||
} | ||
// data, _ := json.Marshal(bird) | ||
data, _ := json.MarshalIndent(bird, " ", " ") | ||
fmt.Println(string(data)) | ||
return string(data) | ||
} | ||
|
||
func LoadAndParceJson() { | ||
jsonData, err := os.ReadFile("example.json") | ||
fmt.Println(err) | ||
var request Request | ||
fmt.Println(json.Unmarshal(jsonData, &request)) | ||
fmt.Println(request) | ||
} | ||
|
||
func LoadAndParseRawMsgToMap() { | ||
jsonData, _ := os.ReadFile("example.json") | ||
var objmap map[string]interface{} | ||
json.Unmarshal(jsonData, &objmap) | ||
fmt.Println(objmap) | ||
} | ||
|
||
func LoadAndParseRawMsg() { | ||
jsonData, _ := os.ReadFile("example.json") | ||
var objmap map[string]json.RawMessage | ||
json.Unmarshal(jsonData, &objmap) | ||
fmt.Println(objmap) | ||
var internalMap map[string]string | ||
json.Unmarshal(objmap["request"], &internalMap) | ||
fmt.Println(internalMap) | ||
} | ||
|
||
func LoadAndParseJsonToCustomStruct() { | ||
jsonData, err := os.ReadFile("custom.json") | ||
fmt.Println(err) | ||
var request RequestTagged | ||
fmt.Println(json.Unmarshal(jsonData, &request)) | ||
fmt.Println(request) | ||
} | ||
|
||
func main() { | ||
ParseJson(CreateJson()) | ||
ParseJsonArrays() | ||
LoadAndParceJson() | ||
LoadAndParseRawMsgToMap() | ||
LoadAndParseRawMsg() | ||
LoadAndParseJsonToCustomStruct() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
} |