golang Marshal和Unmarshal处理json数据

时间:2016-12-26 12:11:51   收藏:0   阅读:1611

{
"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);将数据映射到数组中

 

原文:http://www.cnblogs.com/lixiaoxiao/p/6221647.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!