简体   繁体   English

json解组错误

[英]json Unmarshal error

I'm getting an error of: 我收到以下错误消息:

json.Unmarshal undefined (type interface {} has no field or method Unmarshal)

trying to convert a json byte slice into the generic interface{} type. 尝试将json字节切片转换为通用接口{}类型。 I'm reading the docs for encoding/json and they give an example that shows this is valid. 我正在阅读用于encoding/json的文档,他们给出了一个示例,说明这是有效的。 What gives? 是什么赋予了?

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
)

func main() {

    var json interface{}
    data, _ := ioutil.ReadFile("testMusic.json")
    json.Unmarshal(data, &json)
    m := json.(map[string]interface{})
    fmt.Printf("%+v", m)

}

You've defined a local variable json that masks the global symbol json referring to the JSON module. 您已经定义了一个局部变量json ,该变量掩盖了引用JSON模块的全局符号json Renaming your local variable should allow your code to work. 重命名您的局部变量应允许您的代码工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM