码迷,mamicode.com
首页 > Web开发 > 详细

golang Marshal和Unmarshal处理json数据

时间:2016-12-26 12:14:09      阅读:3142      评论:0      收藏:0      [点我收藏+]

标签:custom   nbsp   als   struct   off   stat   []   code   lis   

{
"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数据

标签:custom   nbsp   als   struct   off   stat   []   code   lis   

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

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!