dongzhi2014 2014-03-26 23:53
浏览 49
已采纳

使用Go转到JSON数组中的特定键

I've had a heck of a time parsing JSON strings and finally landed on https://github.com/bitly/go-simplejson. It looks really promising but it's still giving me an empty result for the following JSON array:

{
 "data": {
  "translations": [
   {
    "translatedText": "Googlebot: Deutsch, um die Luft-Speed-Geschwindigkeit einer unbeladenen Schwalbe?"
   }
  ]
 }
}

I want to get to translatedText by only specifying the key. The reason for this is my JSON structure won't be predictable and so I'd like to target any JSON array but specifying a key without knowing the full structure of the JSON array.

This is the snippet of code I use where content contains the JSON byte array:

f, err := js.NewJson(content)

if err != nil {
    log.Println(err)
}

t := f.Get("translatedText").MustString()

log.Println(t)

t is always blank :( Would appreciate any pointers.

  • 写回答

1条回答 默认 最新

  • dpde7365 2014-03-27 01:27
    关注

    The problem you have is that the function Get does not recursively search through the structure; it only does a look up for the key the at the current level.

    What you can do is to create a recursive function that searches the structure and returns the value once it is found. Below is a working example using the standard package encoding/json:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    // SearchNested searches a nested structure consisting of map[string]interface{}
    // and []interface{} looking for a map with a specific key name.
    // If found SearchNested returns the value associated with that key, true
    // If the key is not found SearchNested returns nil, false
    func SearchNested(obj interface{}, key string) (interface{}, bool) {
        switch t := obj.(type) {
        case map[string]interface{}:
            if v, ok := t[key]; ok {
                return v, ok
            }
            for _, v := range t {
                if result, ok := SearchNested(v, key); ok {
                    return result, ok
                }
            }
        case []interface{}:
            for _, v := range t {
                if result, ok := SearchNested(v, key); ok {
                    return result, ok
                }
            }
        }
    
        // key not found
        return nil, false
    }
    
    
    func main() {
        jsonData := []byte(`{
     "data": {
      "translations": [
       {
        "translatedText": "Googlebot: Deutsch, um die Luft-Speed-Geschwindigkeit einer unbeladenen Schwalbe?"
       }
      ]
     }
    }`)
    
        // First we unmarshal into a generic interface{}
        var j interface{}
        err := json.Unmarshal(jsonData, &j)
        if err != nil {
            panic(err)
        }
    
        if v, ok := SearchNested(j, "translatedText"); ok {
            fmt.Printf("%+v
    ", v)
        } else {
            fmt.Println("Key not found")
        }
    
    }
    

    Result:

    Googlebot: Deutsch, um die Luft-Speed-Geschwindigkeit einer unbeladenen Schwalbe?

    Playground: http://play.golang.org/p/OkLQbbId0t

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作