Go json

Web<--json-->browser,嗯,就是这么个关系

Go里怎么用呢?

1. Marshal 打JSON包

2. NewJson解JSON包

一 第三方库

install:go get github.com/bitly/go-simplejson

二 DEMO

//json.go 

package main

import (
  "fmt"
  "encoding/json"
  "github.com/bitly/go-simplejson"
)

type MyData struct {
  Name string `json:"item"`
  Other float32 `json:"amount"`
}

func main() {
  //code json
  var detail MyData

  detail.Name = "1"
  detail.Other = 2

  body, err := json.Marshal(detail)
  if err != nil {
    panic(err.Error())
  }

  //decode json
  js, err := simplejson.NewJson(body)
  if err != nil {
    panic(err.Error())
  }

  fmt.Println(js)
}

//结果

{"item":"1","amount":2} <nil>
[123 34 105 116 101 109 34 58 34 49 34 44 34 97 109 111 117 110 116 34 58 50 125]
*simplejson.Json:&{map[amount:2 item:1]}
{1 2} <nil>

json也还行

原文地址:https://www.cnblogs.com/woodzcl/p/7568062.html