例如:
{"id":
{"12345678901234":
{"Account":"asdf",
"Password":"qwerty"
"LastSeen":"1397621470",
}
}
}
我一直在尝试制作的一个程序需要获取字符串形式的 ID,然后使用它来检查 LastSeen 中的时间。 我试过使用 simplejson 和 jsonq ,但仍然不知道该怎么做。
最佳答案
您可以使用 RawMessage
并使其更简单 (play with it
):
package main
import (
"encoding/json"
"fmt"
)
var data []byte = []byte(`{"id": {"12345678901234": {"Account":"asdf", "Password":"qwerty", "LastSeen":"1397621470"}}}`)
type Message struct {
Id string
Info struct {
Account string
Password string
LastSeen string
}
}
func main() {
var (
tmpmsg struct {
Data map[string]json.RawMessage `json:"id"`
}
msg Message
)
if err := json.Unmarshal(data, &tmpmsg); err != nil {
panic(err) //you probably wanna use or something instead
}
for id, raw := range tmpmsg.Data {
msg.Id = id
if err := json.Unmarshal(raw, &msg.Info); err != nil {
panic(err)
}
}
fmt.Printf("%+v\n", msg)
}
关于json - 获取 json 动态键名作为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23290618/