Browse Source

学习 真是json 处理

master
VIVIMAN 3 years ago
parent
commit
2b76faa172
  1. 56
      golang_learn/data_func/go_to_json/gjson/main.go
  2. 58
      golang_learn/data_func/go_to_json/sjson/main.go
  3. 2
      golang_learn/go.mod
  4. 9
      golang_learn/go.sum
  5. 2
      gui_gio_learn/haptic/example.go

56
golang_learn/data_func/go_to_json/gjson/main.go

@ -0,0 +1,56 @@
package main
import (
"github.com/tidwall/gjson"
"log"
)
func main() {
log.Print("查询内容:", gjson.Get(jsonStr, "code"))
log.Print("查询内容:", gjson.Get(jsonStr, "data.ext"))
log.Print("查询内容:", gjson.Get(jsonStr, "data.items.0"))
log.Print("查询内容:", gjson.Get(jsonStr, "data.items.2.resourceId"))
}
var jsonStr = `{
"code": 200,
"data": {
"ext": {
"utm_medium": "AAAA"
},
"size": 6,
"items": [
{
"ext": null,
"resourceId": "1",
"mediaAssetInfo": null,
},
{
"ext": null,
"resourceId": "2",
"mediaAssetInfo": null,
},
{
"ext": null,
"resourceId": "3",
"mediaAssetInfo": null,
},
{
"ext": null,
"resourceId": "4",
"mediaAssetInfo": null,
},
{
"ext": null,
"resourceId": "5",
"mediaAssetInfo": null,
},
{
"ext": null,
"resourceId": "6",
"mediaAssetInfo": null,
}
]
},
"message": "success"
}`

58
golang_learn/data_func/go_to_json/sjson/main.go

@ -0,0 +1,58 @@
package main
import (
"github.com/tidwall/sjson"
"log"
)
func main() {
res, err := sjson.Set(jsonStr, "code", "111")
if err != nil {
log.Fatal("发生异常:", err)
return
}
log.Print("查询内容:", res, jsonStr)
}
var jsonStr = `{
"code": 200,
"data": {
"ext": {
"utm_medium": "AAAA"
},
"size": 6,
"items": [
{
"ext": null,
"resourceId": "",
"mediaAssetInfo": null,
},
{
"ext": null,
"resourceId": "",
"mediaAssetInfo": null,
},
{
"ext": null,
"resourceId": "",
"mediaAssetInfo": null,
},
{
"ext": null,
"resourceId": "",
"mediaAssetInfo": null,
},
{
"ext": null,
"resourceId": "",
"mediaAssetInfo": null,
},
{
"ext": null,
"resourceId": "",
"mediaAssetInfo": null,
}
]
},
"message": "success"
}`

2
golang_learn/go.mod

@ -28,6 +28,8 @@ require (
github.com/russross/blackfriday v1.6.0 github.com/russross/blackfriday v1.6.0
github.com/shirou/gopsutil v2.20.4+incompatible github.com/shirou/gopsutil v2.20.4+incompatible
github.com/spf13/viper v1.8.1 github.com/spf13/viper v1.8.1
github.com/tidwall/gjson v1.13.0
github.com/tidwall/sjson v1.2.4
github.com/yeka/zip v0.0.0-20180914125537-d046722c6feb github.com/yeka/zip v0.0.0-20180914125537-d046722c6feb
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/exp/errors v0.0.0-20210715201039-d37aa40e8013 golang.org/x/exp/errors v0.0.0-20210715201039-d37aa40e8013

9
golang_learn/go.sum

@ -308,6 +308,15 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.13.0 h1:3TFY9yxOQShrvmjdM76K+jc66zJeT6D3/VFFYCGQf7M=
github.com/tidwall/gjson v1.13.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.4 h1:cuiLzLnaMeBhRmEv00Lpk3tkYrcxpmbU81tAY4Dw0tc=
github.com/tidwall/sjson v1.2.4/go.mod h1:098SZ494YoMWPmMO6ct4dcFnqxwj9r/gF0Etp19pSNM=
github.com/yeka/zip v0.0.0-20180914125537-d046722c6feb h1:OJYP70YMddlmGq//EPLj8Vw2uJXmrA+cGSPhXTDpn2E= github.com/yeka/zip v0.0.0-20180914125537-d046722c6feb h1:OJYP70YMddlmGq//EPLj8Vw2uJXmrA+cGSPhXTDpn2E=
github.com/yeka/zip v0.0.0-20180914125537-d046722c6feb/go.mod h1:9BnoKCcgJ/+SLhfAXj15352hTOuVmG5Gzo8xNRINfqI= github.com/yeka/zip v0.0.0-20180914125537-d046722c6feb/go.mod h1:9BnoKCcgJ/+SLhfAXj15352hTOuVmG5Gzo8xNRINfqI=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

2
gui_gio_learn/haptic/example.go

@ -46,7 +46,7 @@ func loop(w *app.Window) error {
buzzer.Buzz() buzzer.Buzz()
} }
gtx := layout.NewContext(&ops, e) gtx := layout.NewContext(&ops, e)
layout.Center.Layout(gtx, material.Button(th, &btn, "buzz").Layout) layout.Center.Layout(gtx, material.Button(th, &btn, "测试").Layout)
e.Frame(gtx.Ops) e.Frame(gtx.Ops)
default: default:
ProcessPlatformEvent(e) ProcessPlatformEvent(e)

Loading…
Cancel
Save