【golang】json数据中复杂key的处理

例1

type Transport struct {
	Time  string
	Id    int
}
func main() {
	//将struct的切片包装成json格式
	var st []Transport
	t1 := Transport{Time: "22", Id: 44}
	st = append(st, t1)
	t2 := Transport{Time: "66", Id: 88}
	st = append(st, t2)
	st_json, _ := json.Marshal(st)
	fmt.Println(string(st_json))

	//将json格式解析为struct的切片格式
	var st2 []Transport
	err := json.Unmarshal(st_json, &st2)
	fmt.Println(st2)
	fmt.Println(st2[0].Time)
	fmt.Println(st2[0].Id)
}

例2

json数据:

{
    "friends": [
        {
            "id": 0,
            "name": "Robinson Woods"
        }
    ],
    "parent": [
        {
            "id": 1,
            "name": "Alejandra Mcdaniel"
        }
    ]
}

代码:

package main
 
import (
	"encoding/json"
	"log"
)
 
func main() {
	data := `{
		"friends": [
			{
				"id": 0,
				"name": "Robinson Woods"
			}
		],
		"parent": [
			{
				"id": 1,
				"name": "Alejandra Mcdaniel"
			}
		]
	}`
	type User struct {
		ID   int    `json:"id"`
		Name string `json:"name"`
	}
	dataMap := make(map[string][]User)
	json.Unmarshal([]byte(data), &dataMap)
	for k, v := range dataMap {
		log.Printf(`%v,%v`, k, v)
	}
}

例3

json数据:

{
    "ret":200,
    "data":{
        "32f49e76d7d16b76f0d49f15710b447236acfc90":{
            "torrent":[
                {
                    "sid":2,
                    "torrent_id":55171,
                    "info_hash":"32f49e76d7d16b76f0d49f15710b447236acfc90"
                }
            ]
        },
        "cf7d88fd656d10fe5130d13567aec27068b96676":{
            "torrent":[
                {
                    "sid":10,
                    "torrent_id":36,
                    "info_hash":"8bf47a8baa8bf7927ec61a850959bca8405482f5"
                },
                {
                    "sid":1,
                    "torrent_id":7233,
                    "info_hash":"07bb8defd21288a30a54ad793fe615f81afdbb2b"
                },
                {
                    "sid":8,
                    "torrent_id":5821,
                    "info_hash":"a69b89a8df716982ecceff4b98c532fc538501ae"
                }
            ]
        }
    },
    "msg":"",
    "version":"1.5.0"
}

代码:

package main
 
import (
	"encoding/json"
	"fmt"
)
 
type ApiSitesInfoHash struct {
	Ret  int `json:"ret"`
	Data Data `json:"data"`
	Msg     string `json:"msg"`
	Version string `json:"version"`
}
type Data map[string]Torrents   //自定义类型
type Torrents struct{ //结构体
	Torrent []Torrent `json:"torrent"`
}
type Torrent struct{
	Sid int `json:"sid"`
	Torrent_id int `json:"torrent_id"`
	Info_hash string `json:"info_hash"`
}
 
func main(){
	r:=`{
    "ret":200,
    "data":{
        "32f49e76d7d16b76f0d49f15710b447236acfc90":{
            "torrent":[
                {
                    "sid":2,
                    "torrent_id":55171,
                    "info_hash":"32f49e76d7d16b76f0d49f15710b447236acfc90"
                }
            ]
        },
        "cf7d88fd656d10fe5130d13567aec27068b96676":{
            "torrent":[
                {
                    "sid":10,
                    "torrent_id":36,
                    "info_hash":"8bf47a8baa8bf7927ec61a850959bca8405482f5"
                },
                {
                    "sid":1,
                    "torrent_id":7233,
                    "info_hash":"07bb8defd21288a30a54ad793fe615f81afdbb2b"
                },
                {
                    "sid":8,
                    "torrent_id":5821,
                    "info_hash":"a69b89a8df716982ecceff4b98c532fc538501ae"
                }
            ]
        }
    },
    "msg":"",
    "version":"1.5.0"
}`
 
	var dataMap ApiSitesInfoHash
 
	json.Unmarshal([]byte(r), &dataMap)
	for k, v := range dataMap.Data {
		fmt.Println(k)
		//fmt.Printf("%v",v.Torrent)
		for _,v1:=range v.Torrent{
			fmt.Println(v1.Sid,v1.Torrent_id,v1.Info_hash)
			//fmt.Printf("%v",v1.Sid)
		}
 
	}
}

例4:

json数据:

"Fruit" : {
    "1": {
        "Name": "Apple",
        "PriceTag": "$1"
    },
    "2": {
        "Name": "Pear",
        "PriceTag": "$1.5"
    }
}

代码:

package main
 
import (
    "fmt"
    "encoding/json"
    "time"
 
)
 
func main() {
    type Fruit struct {
        Name string `json:"name"`
        PriceTag string `json:"priceTag"`
    }
 
    type FruitBasket struct {
        Name    string             `json:"name"`
        Fruit   map[string]Fruit   `json:"fruit"`
        Id      int64              `json:"id"`
        Created time.Time          `json:"created"`
    }  
    jsonData := []byte(`
    {
        "Name": "Standard",
        "Fruit" : {
              "1": {
                    "name": "Apple",
                    "priceTag": "$1"
              },
              "2": {
                    "name": "Pear",
                    "priceTag": "$1.5"
              }
        },
        "id": 999,
        "created": "2018-04-09T23:00:00Z"
    }`)
 
    var basket FruitBasket
    err := json.Unmarshal(jsonData, &basket)
    if err != nil {
         fmt.Println(err)
    }
    for _, item := range basket.Fruit {
    fmt.Println(item.Name, item.PriceTag)
    }
}

参考:https://blog.csdn.net/gaoluhua/article/details/104444163

posted @ 2020-08-23 20:53  NetRookieX  阅读(30)  评论(0编辑  收藏  举报