比方说后端应用程序有这样的请求。如您所见,这是一个对象数组。
[
{
"section_id": "8ad1f7cc-a510-48ee-b4fa-bedbee444a84",// (uuid - string)
"section_name": "First section"
},{
"section_id": 1556895,// (int)
"section_name": "Second section"
}
]
我想解析这个数组。根据部分ID的类型,应用程序需要执行不同的操作。如何绕过严格的输入?
requestBody,err := ioutil.ReadAll(request.Body)
if err = json.Unmarshal([]byte(requestBody),§ions); err != nil {
println(err)
}
for _,section := range sections {
if reflect.TypeOf(section.ID) == string {
// block 1
} reflect.TypeOf(section.ID) == int {
// block 2
}
}