当未知JSON数据时:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
)
func readFileFrompath(path string) []byte {
content, err := ioutil.ReadFile(path)
if err != nil {
log.Println(err)
}
return content
}
func main() {
content := readFileFrompath("./vcard.json")
var f interface{}
_ = json.Unmarshal(content, &f)
m := f.(map[string]interface{})
for k, v := range m {
// 处理json数据
fmt.Println(k)
fmt.Println(v)
}
}
其中 vcard.json文件中数据为
{"FirstName":"Jan","LastName":"Kersschot","Addresses":[{"Type":"private","City":"Aartselaar","Country":"Belgium"},{"Type":"work","City":"Boom","Country":"Belgium"}],"Remark":"none"}
结果为
FirstName
Jan
LastName
Kersschot
Addresses
[map[City:Aartselaar Country:Belgium Type:private] map[City:Boom Country:Belgium Type:work]]
Remark
none
因为在学习的过程中,有人提出了相关问题,所以在此随笔记录一下启到抛砖引玉的作用,。