【问题标题】:json Unmarshal errorjson解组错误
【发布时间】:2013-03-19 03:29:38
【问题描述】:

我收到以下错误:

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

尝试将 json 字节切片转换为通用接口{} 类型。我正在阅读encoding/json 的文档,他们举了一个例子来证明这是有效的。什么给了?

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)

}

【问题讨论】:

    标签: json go


    【解决方案1】:

    您已经定义了一个局部变量 json,它掩盖了引用 JSON 模块的全局符号 json。重命名你的局部变量应该可以让你的代码工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多