dpdjv9559 2017-05-11 12:21
浏览 38
已采纳

在Golang中解组数组json

How would I unmarshal something like this:

[
2,
"19223201",
"SomeString",
{
    "SomeField": "FieldValue",
    "SomeField2": "FieldValue2", "SomeFieldN": "FieldValueN",       }
]
  • 写回答

1条回答 默认 最新

  • duanjiao4763 2017-05-11 12:46
    关注

    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)
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?