json反序列化到interface类型后,再转换成具体类型

	expected := model.Item{
		Url: "https://",
		Id:  "1470740797",
		Profile: model.Profile{
			NickName: "乌雲下的黑玫瑰",
			Marriage: "离异",
			Age:      35,
			StarSign: "魔羯座(12.22-01.19)",
			Height:   155,
			Place:    "工作地:阿坝汶川",
		},
	}
	// Item类型
	type Item struct {
	Url string
	Id string
	Profile interface{}
}
var actual model.Item
json.Unmarshal(*resp.Source, &actual)

actual和expected无法比较,因为actual.Profile的类型是interface{}而expected.Profile是Profile类型。
解决;

func FromJsonObj(o interface{}) (profile Profile, err error){
	bytes, err := json.Marshal(o)
	if err != nil {
		return Profile{}, err
	}

	err = json.Unmarshal(bytes, &profile)

	return profile, err
}

actualProfile := FromJsonObj(actual.Profile)
actual.Profile = actualProfile

把actual.Profile的类型是转换成Profile类型

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值