simpleJson处理api返回数据结构不确定的情况

SimpleJson包的简单使用

    • 代码展示
    • 借鉴

代码展示

下面展示一些 内联代码片

// A code block
package jsonPrac

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

func Unmarshal(s string)  {

	res, err := simplejson.NewJson([]byte(s))

	if err != nil {
		fmt.Printf("%v\n", err)
		return
	}

	maps, err := res.Get("orderInfo").Map()
	if err != nil {
		fmt.Printf("%v\n", err)
		return
	}

	for key,value := range maps {
		fmt.Printf("key is %s\n", key)

		if _, ok := value.(map[string]interface{});ok{
			eachOrderMapMap := res.Get("orderInfo").Get(key).MustMap()
			//fmt.Printf("eachOrderMapMap is %v\n", eachOrderMapMap)
			for key1, _ := range eachOrderMapMap {
				eachOrderList :=  res.Get("orderInfo").Get(key).Get(key1).MustArray()
				//fmt.Printf("eachOrderList is %v\n", eachOrderList)
				for i, _ := range eachOrderList {
					eachOrderMap :=  res.Get("orderInfo").Get(key).Get(key1).GetIndex(i)
					//fmt.Printf("eachOrderMap is %v\n", eachOrderMap)
					name := eachOrderMap.Get("receiverName").MustString()
					updateTime := eachOrderMap.Get("updateTime").MustInt()
					fmt.Printf("map name is %s\n", name)
					fmt.Printf("map updateTime is %d\n", updateTime)
				}
			}
		} else if _, ok1 := value.([]interface{}); ok1 {
			eachOrderListList := res.Get("orderInfo").Get(key).MustArray()
			for i1, _ := range eachOrderListList {
				eachOrderList := res.Get("orderInfo").Get(key).GetIndex(i1).MustArray()
				for i2, _ := range eachOrderList {
					eachOrderMap := res.Get("orderInfo").Get(key).GetIndex(i1).GetIndex(i2)
					name := eachOrderMap.Get("receiverName").MustString()
					orderId := eachOrderMap.Get("orderId").MustInt()
					fmt.Printf("list name is %s\n", name)
					fmt.Printf("list orderId is %d\n", orderId)
				}
			}

			//如下方式也能取到,但是应该没有用到simpleJson的优势
			//for _, val := range eachOrderList {
			//	if vall, ok := val.([]interface{}); ok {
			//		for kk, _ := range vall{
			//			if valll, ok1 := vall[kk].(map[string]interface{}); ok1 {
			//				if orderId, ok4 := valll["orderId"].(json.Number); ok4 {
			//					fmt.Printf("list orderId is %s\n", orderId)
			//				}
			//			}
			//		}
			//	}
			//}
		} else {
			fmt.Println("没有合法的结构")
			return
		}
	}
}
// main.go
import (
    "fmt"
	"my_go_practise/selfPractice/jsonPrac"
)

func main() {
   var tempStr1 = `{"orderInfo":{"401720002":[[{"uid":2000064541,"pid":2100830001,"orderId":401720002,"batchId":"20216cca4ecf2fb10afb86a0852bb315","serviceLine":102,"receiverPhone":"13718483694","receiverName":"\u59d3\u540d\u5417","receiverAddress":"\u5317\u4eac \u6d77\u6dc0\u533a \u4e09\u73af\u5230\u56db\u73af\u4e4b\u95f4 \u6211\u4e0d\u544a\u8bc9\u4f60-\u54c8\u54c8","updateTime":1586327726,"sendTime":0,"status":0,"statusName":"\u5f85\u53d1\u8d27","packId":0,"expressNumber":"","sendType":"","expectTime":{"beginTime":"","endTime":""},"materialDetails":[{"materialId":2001457,"materialName":"\u5bfc\u5165 - \u8054\u8c03 - \u7ec4\u5957 - \u9e2d\u9e2d\u82f1\u8bed - 01","quantity":1}]}]]},"expressFlows":[]}`

   var tempStr2 = `{"orderInfo":{"401720002":{"0":[{"status":0,"sendType":"","pid":2100830001,"orderId":401720002,"batchId":"20216cca4ecf2fb10afb86a0852bb315","updateTime":1586327726,"uid":2000064541,"sendTime":0,"serviceLine":102,"receiverName":"姓名吗","statusName":"待发货","expectTime":{"beginTime":"","endTime":""},"materialDetails":[{"materialId":2001457,"materialName":"导入 - 联调 - 组套 - 鸭鸭英语 - 01","quantity":1}],"receiverPhone":"13718483694","receiverAddress":"北京 海淀区 三环到四环之间 我不告诉你-哈哈","packId":0,"expressNumber":""}]}},"expressFlows":[]}`

   jsonPrac.Unmarshal(tempStr2)

   fmt.Println("=======================")

   jsonPrac.Unmarshal(tempStr1)
}
//输出
key is 401720002
map name is 姓名吗
map updateTime is 1586327726
=======================
key is 401720002
list name is 姓名吗
list orderId is 401720002

借鉴

[1] https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/07.2.md

你可能感兴趣的:(golang)