英文:
Unmarshal array json in golang
问题
如何解组类似以下结构的数据:
[
2,
"19223201",
"SomeString",
{
"SomeField": "FieldValue",
"SomeField2": "FieldValue2",
"SomeFieldN": "FieldValueN"
}
]
英文:
How would I unmarshal something like this:
[
2,
"19223201",
"SomeString",
{
"SomeField": "FieldValue",
"SomeField2": "FieldValue2", "SomeFieldN": "FieldValueN", }
]
答案1
得分: 0
你可以使用interface{}
类型,如果你没有定义任何接口,并将其解组为接口类型的切片。
import (
"fmt"
"encoding/json"
)
func main() {
strBytes := []byte(`[2,"19223201", "SomeString",{"SomeField": "FieldValue","SomeField2": "FieldValue2", "SomeFieldN": "FieldValueN"}]`)
keys := make([]interface{},0)
json.Unmarshal(strBytes, &keys)
fmt.Println(keys)
}
请注意,这段代码是用Go语言编写的。
英文:
You can use interface{}
if you have not define any interface and unmarshal into interface type slice.
import (
"fmt"
"encoding/json"
)
func main() {
strBytes := []byte(`[2,"19223201", "SomeString",{"SomeField": "FieldValue","SomeField2": "FieldValue2", "SomeFieldN": "FieldValueN"}]`)
keys := make([]interface{},0)
json.Unmarshal(strBytes, &keys)
fmt.Println(keys)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论