获取 JSON 动态键名作为字符串?

huangapple go评论121阅读模式
英文:

Getting json dynamic key name as string?

问题

例如:

  1. {"id":
  2. {"12345678901234":
  3. {"Account":"asdf",
  4. "Password":"qwerty"
  5. "LastSeen":"1397621470",
  6. }
  7. }
  8. }

我一直在尝试编写一个程序,需要将id作为字符串获取,然后稍后使用它来检查LastSeen中的时间。我尝试使用simplejsonjsonq,但仍然无法弄清楚如何做到这一点。

英文:

For example:

  1. {"id":
  2. {"12345678901234":
  3. {"Account":"asdf",
  4. "Password":"qwerty"
  5. "LastSeen":"1397621470",
  6. }
  7. }
  8. }

A program I've been trying to make needs to get the id as a string and then later use it to check the time in LastSeen.
I've tried using simplejson and jsonq,but still cant figure out how to do that.

答案1

得分: 3

你可以使用RawMessage来简化代码(play with it) :

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. var data []byte = []byte(`{"id": {"12345678901234": {"Account":"asdf", "Password":"qwerty", "LastSeen":"1397621470"}}}`)
  7. type Message struct {
  8. Id string
  9. Info struct {
  10. Account string
  11. Password string
  12. LastSeen string
  13. }
  14. }
  15. func main() {
  16. var (
  17. tmpmsg struct {
  18. Data map[string]json.RawMessage `json:"id"`
  19. }
  20. msg Message
  21. )
  22. if err := json.Unmarshal(data, &tmpmsg); err != nil {
  23. panic(err) //you probably wanna use or something instead
  24. }
  25. for id, raw := range tmpmsg.Data {
  26. msg.Id = id
  27. if err := json.Unmarshal(raw, &msg.Info); err != nil {
  28. panic(err)
  29. }
  30. }
  31. fmt.Printf("%+v\n", msg)
  32. }
英文:

You can use RawMessage and make it much simpiler (play with it) :

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. var data []byte = []byte(`{"id": {"12345678901234": {"Account":"asdf", "Password":"qwerty", "LastSeen":"1397621470"}}}`)
  7. type Message struct {
  8. Id string
  9. Info struct {
  10. Account string
  11. Password string
  12. LastSeen string
  13. }
  14. }
  15. func main() {
  16. var (
  17. tmpmsg struct {
  18. Data map[string]json.RawMessage `json:"id"`
  19. }
  20. msg Message
  21. )
  22. if err := json.Unmarshal(data, &tmpmsg); err != nil {
  23. panic(err) //you probably wanna use or something instead
  24. }
  25. for id, raw := range tmpmsg.Data {
  26. msg.Id = id
  27. if err := json.Unmarshal(raw, &msg.Info); err != nil {
  28. panic(err)
  29. }
  30. }
  31. fmt.Printf("%+v\n", msg)
  32. }

答案2

得分: 1

在Golang博客上关于JSON的文章中,可以使用encoding/json包来实现。我创建了一个小程序来实现这个功能,代码如下:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. var data []byte = []byte(`{"id": {"12345678901234": {"Account":"asdf", "Password":"qwerty", "LastSeen":"1397621470"}}}`)
  7. type Message struct {
  8. id string
  9. LastSeen int64
  10. }
  11. var m Message
  12. func main() {
  13. var i interface {}
  14. err := json.Unmarshal(data, &i)
  15. if err != nil {
  16. println("Error decoding data")
  17. fmt.Printf("%s", err.Error())
  18. return
  19. }
  20. m := i.(map[string]interface{})
  21. for k, v := range m {
  22. println(k)
  23. im := v.(map[string]interface{})
  24. for ik, iv := range im {
  25. println("\t", ik)
  26. jm := iv.(map[string]interface{})
  27. for jk, jv := range jm {
  28. println("\t\t", jk, ": ", jv.(string))
  29. }
  30. }
  31. }
  32. }

如果这段代码在Go的最佳实践方面存在问题,我向你道歉,因为我对这门语言还不熟悉。我知道其中有一些部分并不是完全必要的,比如Message类型的定义,但至少在你的数据上是可以工作的。

英文:

Looking at the Golang blog post on JSON <a href="http://blog.golang.org/json-and-go">here</a> it can be done using the encoding/json package. I created a small program to do this as follows:

  1. package main
  2. import (
  3. &quot;encoding/json&quot;
  4. &quot;fmt&quot;
  5. )
  6. var data []byte = []byte(`{&quot;id&quot;: {&quot;12345678901234&quot;: {&quot;Account&quot;:&quot;asdf&quot;, &quot;Password&quot;:&quot;qwerty&quot;, &quot;LastSeen&quot;:&quot;1397621470&quot;}}}`)
  7. type Message struct {
  8. id string
  9. LastSeen int64
  10. }
  11. var m Message
  12. func main() {
  13. var i interface {}
  14. err := json.Unmarshal(data, &amp;i)
  15. if err != nil {
  16. println(&quot;Error decoding data&quot;)
  17. fmt.Printf(&quot;%s&quot;, err.Error())
  18. return
  19. }
  20. m := i.(map[string]interface{})
  21. for k, v := range m {
  22. println(k)
  23. im := v.(map[string]interface{})
  24. for ik, iv := range im {
  25. println(&quot;\t&quot;, ik)
  26. jm := iv.(map[string]interface{})
  27. for jk, jv := range jm {
  28. println(&quot;\t\t&quot;, jk, &quot;: &quot;, jv.(string))
  29. }
  30. }
  31. }
  32. }

I apologise if this is poor in terms of Go best practices and such, I am new to the language. And I know that some elements of this aren't entirely necessary like the Message type definition but this works, at least on your data.

huangapple
  • 本文由 发表于 2014年4月25日 18:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/23290618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定