Json Golang错误:接口转换:接口{}是bool/float…,不是字符串
我试图使用Golang解码任意JSON,因此我在map[string]接口{}中解组传入的JSON,如下代码所示:Json Golang错误:接口转换:接口{}是bool/float…,不是字符串,json,go,Json,Go,我试图使用Golang解码任意JSON,因此我在map[string]接口{}中解组传入的JSON,如下代码所示: func JsonHandler(jsonRequest []byte) { // Creating the maps for JSON var m interface{} // Parsing/Unmarshalling JSON encoding/json if err := json.Unmarshal([]byte
func JsonHandler(jsonRequest []byte) {
// Creating the maps for JSON
var m interface{}
// Parsing/Unmarshalling JSON encoding/json
if err := json.Unmarshal([]byte(jsonRequest), &m); err != nil {
panic(err)
}
//Creating an output file for writing
f, err := os.OpenFile("/home/dorrahadrich/Desktop/output.txt", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
ParseJson(m, f, err)
}
func ParseJson(m interface{}, f *os.File, err error) {
switch v := m.(interface{}).(type){
case map[string]interface{}:
ParseMap (m.(map[string]interface{}),f,err)
fmt.Println(v)
case []interface{}:
ParseArray (m.([]interface{}),f,err)
fmt.Println(v)
default:
}
}
func ParseMap(aMap map[string]interface{}, f *os.File, err error) {
for key, val := range aMap {
switch val.(type) {
case map[string]interface{}:
if _, err = f.WriteString(key + "={\n"); err != nil {
panic(err)
}
ParseMap(val.(map[string]interface{}), f, err)
//Close brackets
if _, err = f.WriteString("};\n"); err != nil {
panic(err)
}
case []interface{}:
//Write to file
if _, err = f.WriteString(key + "={\n"); err != nil {
panic(err)
}
ParseArray(val.([]interface{}), f, err)
//Close brackets
if _, err = f.WriteString("};\n"); err != nil {
panic(err)
}
default:
otherValues(key, val.(interface{}), f , err)
}
}
}
func ParseArray(anArray []interface{}, f *os.File, err error) {
for _, val := range anArray {
switch val.(type) {
case map[string]interface{}:
ParseMap(val.(map[string]interface{}), f, err)
case []interface{}:
ParseArray(val.([]interface{}), f, err)
default:
}
}
}
func otherValues(key string, other interface{}, f *os.File, err error) {
if _, err = f.WriteString(key); err != nil {
panic(err)
}
if _, err = f.WriteString("="); err != nil {
panic(err)
}
switch other.(interface{}).(type) {
case string:
if _, err = f.WriteString(other.(string)); err != nil {
panic(err)
}
case float64:
if _, err = f.WriteString(strconv.FormatFloat(other.(float64), 'f', -1, 64)); err != nil {
panic(err)
}
case bool:
if _, err = f.WriteString(strconv.FormatBool(other.(bool))); err != nil {
panic(err)
}
default:
}
}
问题是,每当JSON包含bool/int/float或任何not字符串值时,程序就会惊慌失措地说它无法将接口转换为给定类型!请注意,JSON是任意的,因此我对键和值一无所知,我无法将其解压缩到接口中,也无法访问提供路径的值。错误说明了一切:
接口转换:接口{}为bool/float64
在解组json时,int和bool的值不是接口类型。在您的开关中,也为bool/float64/string添加case。由于json是任意的,所以使用接口{}对其进行解组
func otherValues(other interface{}, f *os.File, err error) {
switch bb := other.(interface{}).(type) {
case string:
fmt.Println("This is a string")
case float64:
fmt.Println("this is a float")
case bool:
fmt.Println("this is a boolean")
default:
fmt.Printf("Default value is of type %v", bb)
}
}
替代file.WriteString使用
func (f *File) Write(b []byte) (n int, err error)
Write将len(b)字节写入文件。它返回字节数
如果有错误,请输入错误信息。当n=
len(b)
解组为
接口{}
类型的简单值。这对你有用吗?任何有效的JSON都可以解组到该接口中。这是因为当您将JSON解组到接口{}时,它还可以包含bool/float64/string。您必须为同一个tooI添加案例我尝试切换“val”的类型并将其转换为字符串,然后再将其传递给f.WriteString函数,仍然是相同的错误!!任何失败的json示例都有助于您不处理字符串、浮点和布尔。我将json解组到接口{}中,然后对接口{}的实际类型进行切换,并按照您所说的添加案例,但我仍然得到相同的错误。显示正在解组的json以及在传递数据后尝试的代码。您是否遇到相同的错误可能是您正在使用file.WriteString,这就是为什么可以写入字符串的原因。您可以使用file.WriteI。如果您在代码中发现错误并对其进行了编辑。我们需要将other.(bool)
转换为other.(接口{})。(bool)
但在使用file.WriteString之前,我正在将值转换为字符串!