解析显式数组 [英] Parsing explicit arrays
问题描述
[[1,a, b,2, 000000,[1,2,3],[1,2,3]], X, Y, Z,[1,2,3],[1 ,2,3]]]]
除了为这类消息编写我自己的hack'ish解析器有没有一种标准的方式来解释这一点,我不知道? 你的输入是一个 JSON 字符串。在Go中,您可以使用 编码/ json
解码它。
通常,当JSON字符串的结构先前已知时,Go struct
type可以构建它的模型,然后你可以解组为 struct
类型的值。
如果结构未知或变化,可以解组为 interface {}
类型的值,它可以是任何JSON数据的目标:
s:=`[[1,a,b,2,000000,[[1,2,3],[ 1,2,3]],x,y,z,[[1,2,3],[1,2,3]]]]`
var v interface {}
if err:= json.Unmarshal([] byte(s),& v); err!= nil {
fmt.Println(err)
return
}
fmt.Println(v)
fmt.Printf(%#v\\\
,v)
[[1 ab 2 000000 [[1 2 3] [1 2 3]] xyz [[1 2 3] [1 2 3]]]]
[] interface {} {[] interface {} { ,b,2,000000,[] interface {} {[] interface {},1,2,3},[] interface {},1,2,3}},x,y ,z,[] interface {} {[] interface {} {1,2,3},[] interface {} {1,2,3}}}}
正如您所见,结果是一片切片,其中包含不同类型的元素(JSON数字,字符串和更多切片)。>使用Go的 这里有相同的输出添加缩进以获得更好的感觉nofollow>复合文字
[] interface {} {
[] interface {} {
1, a,b,2,000000,
[] interface {} {
[] interface {},1,2,3},
[] interface {} {1,2,3}},
x,y,z,
[] interface {} {
[] interface {} {1,2,3 },
[] interface {} {1,2,3},
},
},
}
当然,这不是很方便,因为您必须使用 type assertion 从这个generic类型的值中提取值,这个值类型为 interface {}
,例如提取前2个非数组值(也打印他们的类型进行验证):
fmt.Printf(%T%[1] v \\\
, v。([] interface {})[0]。([] interface {})[0])
fmt.Printf(%T%[1] v \\\
,v。([接口{})[0]。([] interface {})[1])$ b $ b
float64 1
字符串a
值得注意的是JSON数字i n Go被解组到 float 64
类型的值,尽管它们可能是整数(除非明确指定另一个类型)。
I'm trying to parse this type of response from a server:
[[1,"a","b",2,"000000",[[1,2,3],[1,2,3]],"x","y","z",[[1,2,3],[1,2,3]]]]
Besides writing my own hack'ish parser for this type of messages is there a standard way to interpret this that I'm not aware of?
Your input is a JSON string. In Go, you can use the encoding/json
package to decode it.
Usually, when the structure of the JSON string is known prior, a Go struct
type can be constructed which models it, and then you can unmarshal into a value of that struct
type.
If the structure is unknown or is varying, you may unmarshal into a value of type interface{}
which can be the target of any JSON data:
s := `[[1,"a","b",2,"000000",[[1,2,3],[1,2,3]],"x","y","z",[[1,2,3],[1,2,3]]]]`
var v interface{}
if err := json.Unmarshal([]byte(s), &v); err != nil {
fmt.Println(err)
return
}
fmt.Println(v)
fmt.Printf("%#v\n", v)
Output (try it on the Go Playground):
[[1 a b 2 000000 [[1 2 3] [1 2 3]] x y z [[1 2 3] [1 2 3]]]]
[]interface {}{[]interface {}{1, "a", "b", 2, "000000", []interface {}{[]interface {}{1, 2, 3}, []interface {}{1, 2, 3}}, "x", "y", "z", []interface {}{[]interface {}{1, 2, 3}, []interface {}{1, 2, 3}}}}
As you can see, the result is a slice of slices, with elements of varying types (JSON numbers, strings and even more slices).
Here's the same output adding indentation to get a better feeling of it (using Go's composite literal syntax):
[]interface{}{
[]interface{}{
1, "a", "b", 2, "000000",
[]interface{}{
[]interface{}{1, 2, 3},
[]interface{}{1, 2, 3}},
"x", "y", "z",
[]interface{}{
[]interface{}{1, 2, 3},
[]interface{}{1, 2, 3},
},
},
}
Of course this is not very convenient, as you have to use type assertion to "extract" values from this "generic" value of type interface{}
, for example to extract the first 2 non-arrays values (also printing their types for verification):
fmt.Printf("%T %[1]v\n", v.([]interface{})[0].([]interface{})[0])
fmt.Printf("%T %[1]v\n", v.([]interface{})[0].([]interface{})[1])
Output:
float64 1
string a
Worth noting that JSON numbers in Go are unmarshaled to a value of type float64
even though they could be integers (unless another type is explicitly specified).
这篇关于解析显式数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!