- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
查看这个 Playground :http://play.golang.org/p/dWku6SPqj5
基本上,我正在处理的库接收一个接口(interface){}
作为参数,然后需要从字节数组中json.Unmarshal
。在幕后,interface{}
参数是一个与字节数组的 json 结构相匹配的结构,但库没有对该结构的引用(但它确实有对相应的引用reflect.Type through).
为什么json包检测不到底层类型?出于某种原因,它返回一个简单的映射而不是实际的结构。
代码如下:
package main
import "fmt"
import "encoding/json"
import "reflect"
func main() {
good()
bad()
}
func good() {
var ping Ping = Ping{}
deserialize([]byte(`{"id":42}`), &ping)
fmt.Println("DONE:", ping.ID)
}
func bad() {
var ping interface{} = Ping{}
deserialize([]byte(`{"id":42}`), &ping)
fmt.Println("DONE:", ping) // It's a simple map now, not a Ping. Why?
}
func deserialize(stuff []byte, thing interface{}) {
value := reflect.ValueOf(thing)
fmt.Printf("%+v | %v\n", value, value.Kind())
err := json.Unmarshal(stuff, thing)
if err != nil {
panic(err)
}
}
type Ping struct {
ID int `json:"id"`
}
最佳答案
您已将指向抽象接口(interface)的指针传递给 json
。您应该简单地将指针传递给 Ping
作为抽象接口(interface):
func bad() {
var ping interface{} = &Ping{} // <<<< this
deserialize([]byte(`{"id":42}`), ping) // << and this
fmt.Println("DONE:", ping) // It's a simple map now, not a Ping. Why?
}
但是,如果正如您所说,您没有一个指针可以转换为 interface{}
,您可以使用 reflect 创建一个新指针,反序列化到它,然后将值复制回来:
func bad() {
var ping interface{} = Ping{}
nptr := reflect.New(reflect.TypeOf(ping))
deserialize([]byte(`{"id":42}`), nptr.Interface())
ping = nptr.Interface()
fmt.Println("DONE:", ping) // It's a simple map now, not a Ping. Why?
}
关于json - 为什么 json.Unmarshal 返回一个映射而不是预期的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21468741/
json: 无法将数组解码为 Go 类型的值 配置 json: { "monitor_servers_info":[ { "server
我有一个看起来像这样的 JSON blob { "metadata":{ "id":"2377f625-619b-4e20-90af-9a6cbfb80040",
我正在向第 3 方 API 发出 JSON 请求。如果我的身份验证 token 已过期,我会收到 {"error": "message"} .如果一切顺利,我会收到有效的回复。 现在,我调用json.
这个问题在这里已经有了答案: json.Marshal(struct) returns "{}" (3 个答案) 关闭 6 年前。 我在解码从 .json 文件中读取的 json 数据时遇到问题 t
我正在尝试从文件中读取并将其加载到结构 slice 中。如 block 注释中所示,我读入的行已正确加载。 我遇到的问题是 class 变量总是返回空值。我做错了什么? func loadClasse
我很难理解为什么下面使用 unmarshal 方法的代码不起作用,但我用 NewDecoder 编写的代码几乎相同,而且运行良好。 package conf import ( "os"
我目前遇到以下问题: 我通过 websocket 得到一个 []byte/string 看起来像 eventname {"JSON": "data", "In": "different formats
我正在从本地主机读取一个 json 文档并尝试将其转换为 Test 类型: type Test struct { one string two string three str
这个问题在这里已经有了答案: My structures are not marshalling into json [duplicate] (3 个答案) 关闭 6 年前。 我有以下代码: pac
我正在开发一个 RSS 阅读器应用程序,遇到了纽约时报 RSS 提要的问题。我已将问题缩小到以下 XML(省略了不必要的字段): https://www.nytimes.com/2017/09/
我有一个如下所示的 JSON 对象: {"API version":"1.2.3"} 我想使用 json.Unmarshal() 将它转换为一个对象去功能。根据this blog post : How
我有一个像这样的 JSON blob { "metadata":{ "id":"2377f625-619b-4e20-90af-9a6cbfb80040", "
我已经被这个问题困扰了几个星期了。我有一个从 Autonomy IDOL 搜索中收到的 XML 文档,可以将其成功解码为一组 Java 对象。但是,如果有国际字符,例如中文、日文、俄文/西里尔文,它们
我有一个图像数据结构 type ImageData struct { Name string Data []byte } 数据字段是转换为字节的图像。 我有jsonImages和[{"
这不是Stop json.Marshal() from stripping trailing zero from floating point number的副本,因为我希望和编码(marshal)(
在此链接:JAXB Unmarshalling XML string - Looping through all tags有人提出了一个很好的解决方案。我试图从中受益,但我无法让它在我的情况下发挥作用
我正在尝试解码一个没有注释的编码对象。 这是我的对象类。 class Student { private String name; private int age; publi
我在各种堆栈溢出帖子和博客条目中看到了以下语法: JAXBElement sc = unmarshaller.unmarshal(is, SomeClass.class); 那么当我尝试使用此语法时,
我必须在 Java 对象中解析 XML,并且 XML 包含一个未包装的对象列表,如下所示: Main Property A Main Property B
刚开始使用 Go,我对我正在学习的教程有一点疑问。我读到 Unmarshall 是某种 JSON 编码,我的疑问是:err = json.Unmarshal(body, &p) 为什么我们要将编码后的
我是一名优秀的程序员,十分优秀!