json
Examples of how to parse (unmarshal) and stringify (marshal) JSON.
Node.js
let jsonstr = '{"foo":"bar"}'
let parsed = JSON.parse(jsonstr)
console.log(parsed)
jsonstr = JSON.stringify(parsed)
console.log(jsonstr)
Output
{ foo: 'bar' }
{"foo":"bar"}
Go
package main
import (
"encoding/json"
"fmt"
)
type T struct {
Foo string `json:"foo"`
}
func main() {
jsonstr := `{"foo":"bar"}`
t := new(T)
err := json.Unmarshal([]byte(jsonstr), t)
if err != nil {
panic(err)
}
fmt.Println(t)
marshalled, err := json.Marshal(t)
jsonstr = string(marshalled)
fmt.Println(jsonstr)
}
Output
&{bar}
{"foo":"bar"}
当前内容版权归 miguelmota 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 miguelmota .