当前位置: 首页 > 工具软件 > SimpleJSON > 使用案例 >

go 学习 go-simplejson 遍历

师谦
2023-12-01

方式一

		dataJson := jsonInfo.Get("tick").Get("data")
		dataArr, _ := dataJson.Array()

		// 遍历
		for i := range dataArr {
			info := dataJson.GetIndex(i)
			priceF := info.Get("price").MustFloat64()
			amountF := info.Get("amount").MustFloat64()
			direction := info.Get("direction").MustString()

		}

方式二


		//	var priceF float64
		//	var amountF float64
		//	var direction string
		//
		//	if eachMap, ok := data.(map[string]interface{}); ok {
		//		//fmt.Println(reflect.TypeOf(each_map["direction"]))
		//
		//		if price, ok := eachMap["price"].(json.Number); ok {
		//			priceFloat, err := price.Float64()
		//			if err == nil {
		//				priceF = priceFloat
		//			}
		//		}
		//
		//		if amount, ok := eachMap["amount"].(json.Number); ok {
		//			amountFloat, err := amount.Float64()
		//			if err == nil {
		//				amountF = amountFloat
		//			}
		//		}
		//
		//		if d, ok := eachMap["direction"].(string); ok {
		//			direction = d
		//		}
		//
		//	}
		//

		//
		//}

方式三


		//
		//		var priceF float64
		//		var amountF float64
		//		var direction string
		//
		//		for k, v := range data.(map[string]interface{}) {
		//			//out.Set(k, v)
		//			if "price" == k{
		//				price := v.(json.Number)
		//				if p, err := price.Float64() ;err != nil {
		//					fmt.Println(err)
		//				} else {
		//					priceF = p
		//				}
		//			}
		//
		//			if "amount" == k{
		//				amount := v.(json.Number)
		//				if p, err := amount.Float64() ;err != nil {
		//					fmt.Println(err)
		//				} else {
		//					amountF = p
		//				}
		//			}
		//
		//			if "direction" == k{
		//				direction = v.(string)
		//			}
		//		}
		//
		//
		//})

 类似资料: