如何使用Golang中的JSON解组嵌套数组中的键值对?

huangapple go评论76阅读模式
英文:

How to Unmarshal the pair values in an nested array by json for Golang

问题

以下是翻译好的内容:

以下是类似以下的JSON数据:

{"xxx_xxx":{"asks":[[0.00000315,1022.53968253],[0.00000328,200],[0.00000329,181.70008541]],"bids":[[0.00000254,2685.36319716],[0.00000253,600],[0.0000025,1000]]}}

结构如下:

type Depth struct {
XXX_XXX struct {
Asks []struct {
Num0 float64 json:"0"
Num1 float64 json:"1"
} json:"asks"
Bids []struct {
Num0 float64 json:"0"
Num1 float64 json:"1"
} json:"bids"
} json:"xxx_xxx"
}

在Go语言中尝试映射如下:

json.Unmarshal(r, &depth)

将得到以下全零数组:

{{[{0 0} {0 0} {0 0} ] [{0 0} {0 0} {0 0} ]}}

它没有得到我期望的结果。如何解决这个问题?

这是Playground上的代码:
https://play.golang.org/p/wxFV6Mv26t

英文:

The JSON data like the following:

{"xxx_xxx":{"asks":[[0.00000315,1022.53968253],[0.00000328,200],[0.00000329,181.70008541]],"bids":[[0.00000254,2685.36319716],[0.00000253,600],[0.0000025,1000]]}}

the struct as:

type Depth struct {
	XXX_XXX struct {
		Asks []struct {
			Num0 float64 `json:"0"`
			Num1 float64 `json:"1"`
		} `json:"asks"`
		Bids []struct {
			Num0 float64 `json:"0"`
			Num1 float64 `json:"1"`
		} `json:"bids"`
	} `json:"xxx_xxx"`
}

when try to map in go-lang as following:

json.Unmarshal(r, &depth)

will get all zero arrays like following:

{{[{0 0} {0 0} {0 0} ] [{0 0} {0 0} {0 0} ]}}

It does not come up the result as I expected.
How to solve this problem?

HERE is the code on playground:
https://play.golang.org/p/wxFV6Mv26t

答案1

得分: 3

问价和盘价是数组类型而不是结构类型。

这是一个可工作的示例代码

下面是一个示例,添加了一个Nums类型,该类型具有OneTwo方法,您可以使用这些方法,因为它们似乎是您期望的格式。您还可以在这些方法中添加一些错误检查,以确保格式正确。

type Depth struct {
    XXX XXX `json:"xxx_xxx"`
}

type XXX struct {
    Asks []Nums `json:"asks"`
    Bids []Nums `json:"bids"`
}

type Nums []float64

func (n Nums) One() float64 {
    if len(n) > 0 {
        return n[0]
    }
    return 0
}

func (n Nums) Two() float64 {
    if len(n) > 1 {
        return n[1]
    }
    return 0
}
英文:

Asks and bids are array types not struct types.

Here is a working playground example

Below is an example that adds a Nums type that has a One and Two method you can use since those seem to be the format you are expecting. You could add some error checking in these methods to ensure the format is correct too.,

type Depth struct {
    XXX XXX `json:"xxx_xxx"`
}

type XXX struct {
    Asks []Nums `json:"asks"`
 	Bids []Nums `json:"bids"`
}

type Nums []float64

func (n Nums) One() float64 {
    if len(n) > 0 {
	     return n[0]
    }
    return 0
}

func (n Nums) Two() float64 {
    if len(n) > 1 {
	    return n[1]
    }
    return 0
}

答案2

得分: 2

我认为这是因为你的Go语言中的数组是结构体,但是你的JSON中有数组的数组,它们并不相同。你需要将[0.00000315,1022.53968253]改为{"0": ##, "1": ##},以此类推。

或者,你可以创建一个单独的Go结构体,其中Asks是一个包含多个float64数组的数组,但这样做可能会降低类型安全性,因为数组的元素数量可能多于或少于2个。

在JavaScript中,数组元素可以通过数字键引用,数组可以被视为对象,但在JSON中,对象和数组是不同的。

编辑:根据下面的评论,添加键名周围的引号。我的错误 如何使用Golang中的JSON解组嵌套数组中的键值对?

英文:

I believe it's because your arrays are structs in Go, but your JSON has arrays of arrays, which aren't the same. You'd want to change [0.00000315,1022.53968253] to {"0": ##, "1": ##} , so on and so forth.

Or, you can have a separate Go struct where Asks is an array of arrays of float64s, etc., but this would be less type safe since arrays could have more or less than 2 elements.

Now, in JS, array elements can be referenced by numerical keys and arrays can be treated as objects, but in JSON, objects and arrays are distinct.

EDIT: add quotations around key names, as specified by the comment below. My mistake 如何使用Golang中的JSON解组嵌套数组中的键值对?

huangapple
  • 本文由 发表于 2017年3月20日 06:18:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/42892984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定