some json
This commit is contained in:
parent
28bf737ac7
commit
505a4ee58a
8
README
8
README
@ -47,7 +47,6 @@ gobyexample.com signups
|
|||||||
* gzip
|
* gzip
|
||||||
* listing files
|
* listing files
|
||||||
* regular expressions
|
* regular expressions
|
||||||
* json parsing/unparsing
|
|
||||||
* tls server
|
* tls server
|
||||||
* tls client
|
* tls client
|
||||||
* https server
|
* https server
|
||||||
@ -79,3 +78,10 @@ gobyexample.com signups
|
|||||||
* time
|
* time
|
||||||
* oauth for google domains
|
* oauth for google domains
|
||||||
* connection pool
|
* connection pool
|
||||||
|
* typed json parse/unparse
|
||||||
|
|
||||||
|
= program ideas
|
||||||
|
command line client for public json api
|
||||||
|
redis server
|
||||||
|
github webook receiver
|
||||||
|
campfire bot
|
||||||
|
34
src/xx-json.go
Normal file
34
src/xx-json.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import ("encoding/json"; "fmt")
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// data to bytes/string
|
||||||
|
bol, _ := json.Marshal(true)
|
||||||
|
fmt.Println(string(bol))
|
||||||
|
|
||||||
|
num, _ := json.Marshal(1)
|
||||||
|
fmt.Println(string(num))
|
||||||
|
|
||||||
|
str, _ := json.Marshal("gopher")
|
||||||
|
fmt.Println(string(str))
|
||||||
|
|
||||||
|
arr, _ := json.Marshal([]string{"apple", "peach", "pear"})
|
||||||
|
fmt.Println(string(arr))
|
||||||
|
|
||||||
|
hsh, _ := json.Marshal(map[string]int{"apple": 5, "lettuce": 7})
|
||||||
|
fmt.Println(string(hsh))
|
||||||
|
|
||||||
|
// string to data
|
||||||
|
byt := []byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`)
|
||||||
|
var dat map[string]interface{}
|
||||||
|
err := json.Unmarshal(byt, &dat)
|
||||||
|
if err != nil { panic(err) }
|
||||||
|
fmt.Println(dat)
|
||||||
|
|
||||||
|
name := dat["Name"].(string)
|
||||||
|
fmt.Println(name)
|
||||||
|
|
||||||
|
parents := dat["Parents"].([]interface{})
|
||||||
|
fmt.Println(parents)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user