golang Marshal和Unmarshal处理json数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang Marshal和Unmarshal处理json数据相关的知识,希望对你有一定的参考价值。
{
"code": 0,
"consume": 201,
"data": {
"custom_name": "神秘梦想",
"official_dream": [
{
"id": "7tj0g",
"description": "眼镜1",
"target_rmb": 60000,
"status": 1
},
{
"id": "84Tou",
"description": "飞机",
"target_rmb": 100000,
"status": 1
},
{
"id": "",
"description": "电视机dvd",
"target_rmb": 200000,
"status": 1
},
{
"id": "o55xl",
"description": "飞机",
"target_rmb": 100000,
"status": 1
},
{
"id": "",
"description": "耳机",
"target_rmb": 2000,
"status": 1
}
]
},
"msg": "OK",
"time": 1482394019
}
将json中的official_dream数据放入定义的数组中,
首先定义一个
type Animal struct {
Id string
Description string
TargetRmb int
Status int
}类型是根据你json的类型定义,然后var animals []Animal ;
b, err := json.Marshal(dreamlist["official_dream"]) ;
将interface转化成字节流;
val := json.Unmarshal(b, &animals);将数据映射到数组中
以上是关于golang Marshal和Unmarshal处理json数据的主要内容,如果未能解决你的问题,请参考以下文章
golang Marshal / Unmarshal将不同的JSON对象转换为Go结构
golang里的json marshal && unmarshal