Go 如何将(type*common.MapStr)传递给类型[]字节?
对不起,如果这个问题太新,因为我昨天才开始学习围棋。 我尝试将publishEvent转换为字节,编译器显示如下错误:Go 如何将(type*common.MapStr)传递给类型[]字节?,go,Go,对不起,如果这个问题太新,因为我昨天才开始学习围棋。 我尝试将publishEvent转换为字节,编译器显示如下错误: cannot convert publishEvent (type *common.MapStr) to type []byte 谁能给我指路吗 多谢各位 var parsed map[string]interface{} bytes := []byte(publishEvent) --->Error occur here err := json
cannot convert publishEvent (type *common.MapStr) to type []byte
谁能给我指路吗
多谢各位
var parsed map[string]interface{}
bytes := []byte(publishEvent) --->Error occur here
err := json.Unmarshal(bytes, &parsed)
if err != nil{
fmt.Println("error: ", err)
}
我假设您使用的结构是common.MapStr from common.MapStr已经是一个map[string]接口{},所以我不确定为什么要将它转换成JSON,然后再将它解析回相同的结构,但如果这是您真正想要做的,请将错误行替换为: 字节,错误:=json.marshallpublishevent 应该有用。下一行关于重新声明err的内容会出现错误,请将其更改为: err=json.Unmarshalbytes,&解析 导致以下代码还添加了另一个错误检查:
var parsed map[string]interface{}
bytes, err := json.Marshal(publishEvent)
if err != nil{
fmt.Println("error: ", err)
// you'll want to exit or return here since we can't parse `bytes`
}
err = json.Unmarshal(bytes, &parsed)
if err != nil{
fmt.Println("error: ", err)
}
非常感谢。我希望它在map中,这样我就可以像fmt.printlnsparsed[id.resp_h].string那样调用它来获取一些值。hi@ctcherry。可以在json.Marshal之前进行字符串替换吗?因为我需要过滤一些不需要的字符串。请帮帮我。谢谢,您可以用stdlib字节查找/替换[]字节。替换函数就是这个意思吗?