golang 复杂结构json转为map

1、存在json文件:j.json, 内容如下:

{
    "aa":{"code":205,"hot":1},
    "bb":{"code":206,"hot":1},
    "cc":{"code":207,"hot":1},
    "dd":{"code":208,"hot":1},
    "ee":{"code":209,"hot":1},
    "ff":{"code":210,"hot":1},
    "gg":{"code":211,"hot":1},
    "hh":{"code":212,"hot":1},
    "ii":{"code":213,"hot":1}
}

2、同目录下 main.go,编写读取解析代码:

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)

func readFile() {
	b, err := ioutil.ReadFile("./j.json")
	if err != nil {
		fmt.Println(err)
	}
	whitelist := map[string]map[string]int{}
	err = json.Unmarshal(b, &whitelist)
	if err != nil {
		fmt.Println(err)
	}

	for key, value := range whitelist {
		fmt.Println("key:", key, "code:", value["code"], "hot:", value["hot"])
	}
}

func main() {
	readFile()
}

  注:初学golang,如有不妥之地,请各位大神指教~

原文地址:https://www.cnblogs.com/Frange/p/13331258.html