一个好用的点 json.RawMessage

比如你是一个go应用,要从一个php的服务获取数据,php是弱类型,很可能会返回(比如透传)1和"1"。

但对于go这样的强类型语言,该如何定义结构体来接收这种不确定类型的响应?

此时可以用encoding/json包里的json.RawMessage类型。具体如下:

package main

import (
    "encoding/json"
    "fmt"
    "reflect"
    "strconv"
)

/*
sometimes we don't know what a field's specific value, the value could be 1 or "1",
so how we can define our strongly typed struct ?

json.RawMessage is a good choice.

*/
type MsgCollect struct {
    From json.RawMessage `json:"from"`
}

func main() {

    str := `{"from":"1"}`
    msgCol := &MsgCollect{}
    json.Unmarshal([]byte(str), &msgCol)

    fmt.Printf("%+v\n", msgCol)

    fmt.Println(string(msgCol.From))
    fmt.Println("1")

    fmt.Println("type: ", reflect.TypeOf(msgCol.From))

    // convert to int
    fromString := string(msgCol.From)
    length := len(fromString)
    // trim quotes 
    fromInt, _ := strconv.Atoi(fromString[1 : length-1])
    fmt.Println(fromInt, " , type: ", reflect.TypeOf(fromInt))

    // convert to string
    fromStr := string(msgCol.From)
    fmt.Println(fromStr, " , type: ", reflect.TypeOf(fromStr))

    //======================================================
    str = `{"from":1}`
    //msgCol = &MsgCollect{}
    json.Unmarshal([]byte(str), &msgCol)

    fmt.Printf("%+v\n", msgCol)

    fmt.Println(string(msgCol.From))

    fmt.Println("type: ", reflect.TypeOf(msgCol.From))

    // convert to int
    fromInt, _ = strconv.Atoi(string(msgCol.From))
    fmt.Println(fromInt, " , type: ", reflect.TypeOf(fromInt))

    // convert to string
    fromStr = string(msgCol.From)
    fmt.Println(fromStr, " , type: ", reflect.TypeOf(fromStr))
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值