英文:
golang - converting [ ]Interface to [ ]strings or joined string
问题
如何将[]interface{}
转换为[]string
,或者将其作为一个连接的字符串返回,其中包含[]interface{}
中的所有元素?下面的屏幕截图显示了类型为[]interface{}
且长度为2的scope
的确切值。在下面的代码中,case
是slice
。目前我正在使用反射来实现这一点,但效果不好。想知道是否有更好的方法来连接一个[]interface{}
元素的切片或数组。
我还尝试使用json unmarshal,像这样"json.Unmarshal([]byte(jwtResp),&mymap)"
,但在将jwt.MapClaims
转换为byte[]
时遇到了类型问题。
parsedkey, _ := jwt.ParseRSAPublicKeyFromPEM([]byte(key))
parsedToken, jwtErr := jwt.Parse(bearerToken, func(token *jwt.Token) (interface{}, error) {
return parsedkey, nil
})
jwtValues = make(map[string]string)
// 我们不知道这里处理的是哪种数据类型,所以使用基于值数据类型的switch case
jwtResp := parsedToken.Claims.(jwt.MapClaims)
for k, v := range jwtResp {
switch reflect.TypeOf(v).Kind() {
case reflect.Slice:
fmt.Println("tp is :", reflect.TypeOf(v)) // 这是 []interface{}
fmt.Println("type is :", reflect.TypeOf(v).Kind()) // 这是 slice
s := reflect.ValueOf(v)
for i := 0; i < s.Len(); i++ {
jwtValues[k] += s.Index(i).Interface().(string)
jwtValues[k] += "|"
}
case reflect.String:
jwtValues[k] = v.(string)
default:
fmt.Println("unknown datatype")
}
}
英文:
How can I convert []interface to []strings or just as a joined single string with all elements in []interface ? Below is the screenshot showing exact value of "scope" which of type []interface with length of 2. In below code, where case is "slice" Currently i am doing this using reflect but this is not looking good. Wondering there should be some good way to join a slice\array of interface elements.
I also did try using json unmarshall like this "json.Unmarshal([]byte(jwtResp),&mymap)" but having trouble with type issues in converting jwt.MapClaims to byte[]
parsedkey, _ := jwt.ParseRSAPublicKeyFromPEM([]byte(key))
parsedToken, jwtErr := jwt.Parse(bearerToken, func(token *jwt.Token) (interface{}, error) {
return parsedkey, nil
})
jwtValues = make(map[string]string)
// we dont know which data types we are dealing with here, so using swtich case based on value data type
jwtResp := parsedToken.Claims.(jwt.MapClaims)
for k, v := range jwtResp {
switch reflect.TypeOf(v).Kind() {
case reflect.Slice:
fmt.Println("tp is : ", reflect.TypeOf(v)) // this is []interface{}
fmt.Println("type is : ", reflect.TypeOf(v).Kind()) // this is slice
s := reflect.ValueOf(v)
for i := 0; i < s.Len(); i++ {
jwtValues[k] += s.Index(i).Interface().(string)
jwtValues[k] += "|"
}
case reflect.String:
jwtValues[k] = v.(string)
default:
fmt.Println("unknown datatype")
}
}
答案1
得分: 1
感谢您的建议。下面是我找到的最接近的解决方案,将 'switch' 替换为 'if' 条件,并添加类型断言。使用下面的代码,我会判断 jwtResp
的值是一个切片、字符串还是其他类型。如果是切片,我会遍历这些值([]interface 类型,其中元素是字符串类型),并将它们连接成一个字符串。
for k, v := range jwtResp {
if s, ok := v.(string); ok {
JwtValues[k] = s
} else if s, ok := v.([]interface{}); ok {
sslice := make([]string, len(s))
for i, v := range s {
sslice[i] = v.(string)
}
JwtValues[k] = strings.Join(sslice, "|")
} else {
logger.Log(util.LogDebug, "unknown data type")
}
}
希望对您有帮助!
英文:
Thanks for the suggestions . Below is the closest solution i found replacing 'switch' with 'if' condition along with type assertions. 'reflect' is costly. Using below code, i am finding if values of jwtResp
is a slice, or string or something else. If slice, traverse through the values([]interface with string type elements) and join those as one concatenated string.
for k, v := range jwtResp {
if s, ok := v.(string); ok {
JwtValues[k] = s
} else if s, ok := v.([]interface{}); ok {
sslice := make([]string, len(s))
for i, v := range s {
sslice[i] = v.(string)
}
JwtValues[k] = strings.Join(sslice, "|")
} else {
logger.Log(util.LogDebug, "unknown data type")
}
}
答案2
得分: 0
我已经翻译好了你提供的代码部分:
不确定是否完全理解了你的问题,但看起来你走在正确的轨道上。
parts := make([]string, s.Len())
for i := 0; i < s.Len(); i++ {
parts = append(parts, s.Index(i).String())
}
jwtValues[k] = strings.Join(parts, "|")
希望对你有帮助!
英文:
Not sure to have fully understood your question, but it seems you're on the right track.
parts := make([]string, s.Len())
for i := 0; i < s.Len(); i++ {
parts = append(parts, s.Index(i).String())
}
jwtValues[k] = strings.Join(parts, "|")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论