Golang嵌套映射过滤

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

Golang nested map filter

问题

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

func main() {
	fmt.Println(fecthData())
}

func fecthData() (map[string]interface{}, error) {
	body := strings.NewReader("dil_kodu=tr")
	req, err := http.NewRequest("POST", "https://www.haremaltin.com/dashboard/ajax/doviz", body)
	if err != nil {
		// 处理错误
		return nil, err
	}
	req.Header.Set("X-Requested-With", "XMLHttpRequest")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		// 处理错误
		return nil, err
	}
	defer resp.Body.Close()
	jsonData, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
		return nil, err
	}

	var data map[string]interface{}
	err = json.Unmarshal(jsonData, &data)
	if err != nil {
		return nil, err
	}

	return data, nil
}

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

响应的一部分如下:

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>
英文:
package main

import (
&quot;encoding/json&quot;
&quot;fmt&quot;
&quot;io/ioutil&quot;
&quot;net/http&quot;
&quot;strings&quot;
)

func main() {
fmt.Println(fecthData())
}

func fecthData() (map[string]interface{}, error) {
body := strings.NewReader(&quot;dil_kodu=tr&quot;)
req, err := http.NewRequest(&quot;POST&quot;, &quot;https://www.haremaltin.com/dashboard/ajax/doviz&quot;, body)
if err != nil {
	// handle err
	return nil, err
}
req.Header.Set(&quot;X-Requested-With&quot;, &quot;XMLHttpRequest&quot;)

resp, err := http.DefaultClient.Do(req)
if err != nil {
	// handle err
	return nil, err
}
defer resp.Body.Close()
jsonData, err := ioutil.ReadAll(resp.Body)
if err != nil {
	panic(err)
	return nil, err
}

var data map[string]interface{}
err = json.Unmarshal(jsonData, &amp;data)
if err != nil {
	return nil, err
}

return data, nil
}

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" 之前添加这个迭代来获取值,但我认为可能有更简单的方法。

英文:
for _, v := range data { // we need value part of the map
	m, ok := v.(map[string]interface{}) // we need the convert the map 
                                        // into interface for iteration
	if !ok {
		fmt.Printf(&quot;Error %T&quot;, v)
	}
	for k, l := range m {
		if k == &quot;ATA_ESKI&quot;{ // the value we want is inside of this map
			a, ok := l.(map[string]interface{}) // interface convert again
			if !ok {
				fmt.Printf(&quot;Error %T&quot;, v)
			}
			for b,c := range a{
				if b == &quot;satis&quot;{ // the value we want
					fmt.Println(&quot;Price is&quot;, c)
				}
			}
		}
	}
}

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:

确定