如何轉換其中包含單個字符串數組的單個字符串。func main() { stringArrayInString := "[\"Hello\",\"Hai\",\"How are you!\"]" //Single string which has string array in it fmt.Println(stringArrayInString) // code to convert to the string array // convertedString}輸出必須是這樣的convertedString[0] = "Hello"convertedString[1] = "Hai"convertedString[2] = "How are you!"如果上面的輸入字符串有 int、string、JSON 類型等混合數據類型是否可以stringArrayInString := "[\"Hello\",\"{\"msg\":\"Hai\"}\",123]"//after convertingconvertedString[0] = "Hello"convertedString[1] = "{\"msg\":\"Hai\"}"convertedString[2] = 123
1 回答

慕運維8079593
TA貢獻1876條經驗 獲得超5個贊
您擁有的字符串數組是一個有效的 JSON 數組,因此您可以執行以下操作:
var convertedString []string
json.Unmarshal([]byte(str),&convertedString)
如果該數組中有多種數據類型,則可以使用字符串數組來做到這一點,您需要一個 interface{} 數組:
var convertedData []interface{}
json.Unmarshal([]byte(str),&convertedData)
然后,您需要檢查該數組中各個元素的類型以找出它們是什么。
- 1 回答
- 0 關注
- 109 瀏覽
添加回答
舉報
0/150
提交
取消