go json字符串 不使用struct如何解析

方法1:使用 原生 json.Unmarshal


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

type JsonController struct {
	app.App
}

var json_str string = `{"rc" : 0,
  "error" : "Success",
  "type" : "stats",
  "progress" : 100,
  "job_status" : "COMPLETED",
  "title" : "商品很好",
  "result" : {
    "total_hits" : 803254,
    "starttime" : 1528434707000,
    "endtime" : 1528434767000,
    "fields" : [ ],
    "timeline" : {
      "interval" : 1000,
      "start_ts" : 1528434707000,
      "end_ts" : 1528434767000,
      "rows" : [ {
        "start_ts" : 1528434707000,
        "end_ts" : 1528434708000,
        "number" : "x12887"
      }, {
        "start_ts" : 1528434720000,
        "end_ts" : 1528434721000,
        "number" : "x13028"
      }, {
        "start_ts" : 1528434721000,
        "end_ts" : 1528434722000,
        "number" : "x12975"
      }, {
        "start_ts" : 1528434722000,
        "end_ts" : 1528434723000,
        "number" : "x12879"
      }, {
        "start_ts" : 1528434723000,
        "end_ts" : 1528434724000,
        "number" : "x13989"
      } ],
      "total" : 803254
    },
      "total" : 8
  }
}`

func (i *JsonController) Decode1Action() {
	var result map[string]interface{}
	unmarshal := json.Unmarshal([]byte(json_str), &result)
	if unmarshal != nil {
		fmt.Println(unmarshal)
	}
	fmt.Printf("%+v", result["error"])
	i2 := result["result"].(map[string]interface{})
	i.Data["json"] = result
	i.Data["need"] = i2["timeline"].(map[string]interface{})["rows"].([]interface{})[0].(map[string]interface{})["number"]
	i.Response()
}

  • 使用map[string]interface{} 接收解析後的數據
  • 然後根據類型 強轉格式 .(map[string]interface{}) 然後取值,再強轉。。。。

方法2 使用simple json 進行解析



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

type JsonController struct {
	app.App
}

var json_str string = `{"rc" : 0,
  "error" : "Success",
  "type" : "stats",
  "progress" : 100,
  "job_status" : "COMPLETED",
  "title" : "商品很好",
  "result" : {
    "total_hits" : 803254,
    "starttime" : 1528434707000,
    "endtime" : 1528434767000,
    "fields" : [ ],
    "timeline" : {
      "interval" : 1000,
      "start_ts" : 1528434707000,
      "end_ts" : 1528434767000,
      "rows" : [ {
        "start_ts" : 1528434707000,
        "end_ts" : 1528434708000,
        "number" : "x12887"
      }, {
        "start_ts" : 1528434720000,
        "end_ts" : 1528434721000,
        "number" : "x13028"
      }, {
        "start_ts" : 1528434721000,
        "end_ts" : 1528434722000,
        "number" : "x12975"
      }, {
        "start_ts" : 1528434722000,
        "end_ts" : 1528434723000,
        "number" : "x12879"
      }, {
        "start_ts" : 1528434723000,
        "end_ts" : 1528434724000,
        "number" : "x13989"
      } ],
      "total" : 803254
    },
      "total" : 8
  }
}`
func (i *JsonController) Decode2Action() {
	jsonObject, err := simplejson.NewJson([]byte(json_str))
	if err != nil {
		fmt.Println(err)
	}

	result := jsonObject.Get("result")
	i.Data["json"] = jsonObject
	i.Data["need"] = result.Get("timeline").Get("rows").GetIndex(0).Get("number")
	i.Response()
}

  • go get github.com/bitly/go-simplejson
  • jsonObject, err := simplejson.NewJson([]byte(json_str))
  • 使用Get 即可
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章