Golang嵌套映射过滤

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

Golang nested map filter

问题

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. )
  9. func main() {
  10. fmt.Println(fecthData())
  11. }
  12. func fecthData() (map[string]interface{}, error) {
  13. body := strings.NewReader("dil_kodu=tr")
  14. req, err := http.NewRequest("POST", "https://www.haremaltin.com/dashboard/ajax/doviz", body)
  15. if err != nil {
  16. // 处理错误
  17. return nil, err
  18. }
  19. req.Header.Set("X-Requested-With", "XMLHttpRequest")
  20. resp, err := http.DefaultClient.Do(req)
  21. if err != nil {
  22. // 处理错误
  23. return nil, err
  24. }
  25. defer resp.Body.Close()
  26. jsonData, err := ioutil.ReadAll(resp.Body)
  27. if err != nil {
  28. panic(err)
  29. return nil, err
  30. }
  31. var data map[string]interface{}
  32. err = json.Unmarshal(jsonData, &data)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return data, nil
  37. }

你可以在上面看到完整的代码,我有一个如下的go响应,它是一个嵌套的map,我想要获取值为34319的"data-ATA5_ESKI-satis"。有人可以帮助我吗?
谢谢你的时间。

响应的一部分如下:

  1. map[data:map[AEDTRY:map[alis:4.6271 code:AEDTRY dir:map[alis_dir: satis_dir:] dusuk:4.7116 kapanis:4.6224 satis:4.7271 tarih:17-06-2022 19:41:45 yuksek:4.7276] AEDUSD:map[alis:0.2680 code:AEDUSD dir:map[alis_dir: satis_dir:] dusuk:0.27 kapanis:0.268 satis:0.2700 tarih:17-06-2022 19:30:02 yuksek:0.27]... ALTIN:map[alis:1024.790 code:ALTIN dir:map[alis_dir:down satis_dir:down] dusuk:1029.05 kapanis:1032.13 satis:1030.650 tarih:17-06-2022 19:41:58 yuksek:1040] ATA5_ESKI:map[alis:33869 code:ATA5_ESKI dir:map[alis_dir:down satis_dir:down] dusuk:34266 kapanis:34112 satis:34319 tarih:17-06-2022 19:41:58 yuksek:34630] XPTUSD:map[alis:933 code:XPTUSD dir:map[alis_dir: satis_dir:] dusuk:936 kapanis:953 satis:936 tarih:17-06-2022 19:41:58 yuksek:957]] meta:map[fiyat_guncelleme:2000 fiyat_yayini:web_socket time:1.655484118278e+12 time_formatted:]] <nil>
英文:
  1. package main
  2. import (
  3. &quot;encoding/json&quot;
  4. &quot;fmt&quot;
  5. &quot;io/ioutil&quot;
  6. &quot;net/http&quot;
  7. &quot;strings&quot;
  8. )
  9. func main() {
  10. fmt.Println(fecthData())
  11. }
  12. func fecthData() (map[string]interface{}, error) {
  13. body := strings.NewReader(&quot;dil_kodu=tr&quot;)
  14. req, err := http.NewRequest(&quot;POST&quot;, &quot;https://www.haremaltin.com/dashboard/ajax/doviz&quot;, body)
  15. if err != nil {
  16. // handle err
  17. return nil, err
  18. }
  19. req.Header.Set(&quot;X-Requested-With&quot;, &quot;XMLHttpRequest&quot;)
  20. resp, err := http.DefaultClient.Do(req)
  21. if err != nil {
  22. // handle err
  23. return nil, err
  24. }
  25. defer resp.Body.Close()
  26. jsonData, err := ioutil.ReadAll(resp.Body)
  27. if err != nil {
  28. panic(err)
  29. return nil, err
  30. }
  31. var data map[string]interface{}
  32. err = json.Unmarshal(jsonData, &amp;data)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return data, nil
  37. }

You can see full code above and I have a go response as below and it is nested map as you see and want to reach "data-ATA5_ESKI-satis" value which is 34319. Is there anybody to help me.
Thank you for your time

A part of response below:

map[data:map[AEDTRY:map[alis:4.6271 code:AEDTRY dir:map[alis_dir: satis_dir:] dusuk:4.7116 kapanis:4.6224 satis:4.7271 tarih:17-06-2022 19:41:45 yuksek:4.7276] AEDUSD:map[alis:0.2680 code:AEDUSD dir:map[alis_dir: satis_dir:] dusuk:0.27 kapanis:0.268 satis:0.2700 tarih:17-06-2022 19:30:02 yuksek:0.27]... ALTIN:map[alis:1024.790 code:ALTIN dir:map[alis_dir:down satis_dir:down] dusuk:1029.05 kapanis:1032.13 satis:1030.650 tarih:17-06-2022 19:41:58 yuksek:1040] ATA5_ESKI:map[alis:33869 code:ATA5_ESKI dir:map[alis_dir:down satis_dir:down] dusuk:34266 kapanis:34112 satis:34319 tarih:17-06-2022 19:41:58 yuksek:34630] XPTUSD:map[alis:933 code:XPTUSD dir:map[alis_dir: satis_dir:] dusuk:936 kapanis:953 satis:936 tarih:17-06-2022 19:41:58 yuksek:957]] meta:map[fiyat_guncelleme:2000 fiyat_yayini:web_socket time:1.655484118278e+12 time_formatted:]] <nil>

答案1

得分: 2

for _, v := range data { // 我们需要地图的值部分
m, ok := v.(map[string]interface{}) // 我们需要将地图转换为接口以进行迭代
if !ok {
fmt.Printf("错误 %T", v)
}
for k, l := range m {
if k == "ATA_ESKI" { // 我们想要的值在这个地图中
a, ok := l.(map[string]interface{}) // 再次进行接口转换
if !ok {
fmt.Printf("错误 %T", v)
}
for b, c := range a {
if b == "satis" { // 我们想要的值
fmt.Println("价格是", c)
}
}
}
}
}

我们可以在最后的 "return data, nil" 之前添加这个迭代来获取值,但我认为可能有更简单的方法。

英文:
  1. for _, v := range data { // we need value part of the map
  2. m, ok := v.(map[string]interface{}) // we need the convert the map
  3. // into interface for iteration
  4. if !ok {
  5. fmt.Printf(&quot;Error %T&quot;, v)
  6. }
  7. for k, l := range m {
  8. if k == &quot;ATA_ESKI&quot;{ // the value we want is inside of this map
  9. a, ok := l.(map[string]interface{}) // interface convert again
  10. if !ok {
  11. fmt.Printf(&quot;Error %T&quot;, v)
  12. }
  13. for b,c := range a{
  14. if b == &quot;satis&quot;{ // the value we want
  15. fmt.Println(&quot;Price is&quot;, c)
  16. }
  17. }
  18. }
  19. }
  20. }

We can get the value adding this iteration before "return data, nil" at the end but I think there must be easier methods for this.

huangapple
  • 本文由 发表于 2022年6月18日 00:56:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/72662582.html
匿名

发表评论

匿名网友

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

确定