然后执行类型断言

然后执行类型断言

本文介绍了解组入接口{}然后执行类型断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一个rabbitmq消息系统得到了一个字符串。在发送之前,我使用 json.Marshal ,将结果转换为 string code>并通过
发送rabbitmq。

我转换和发送的结构可以是:(更改结构的名称和大小,但它应该没关系)

  type Somthing1 struct {
Thing string`json:thing`
OtherThing int64`json:other_thing`
}

  type Somthing2 struct {
Croc int`json:croc`
Odile bool`json:odile`



$ b

该消息完美地作为字符串并在另一面打印
(某些服务器)

直到现在一切正常。
现在我试图将它们转换回它们的结构并声明类型。



第一次尝试是:

  func typeAssert(msg string) {

var input interface {}

json.Unmarshal([] byte(msg),& input)

开关输入。 ){
case Somthing1:
job:= Somthing1 {}
job = input。(Somthing1)
queueResults(job)

case Somthing2:
stats:= Somthing2 {}
stats = input。(Somthing2)
queueStatsRes(stats)
default:
}

这不起作用。在Unmarshaling
之后打印 input 类型时,我得到 map [string] interface {} (? !)



甚至比这更陌生,map key是我得到的字符串,map值是空的。



$ p
$ b

  func typeAssert(msg string){

var input interface {}

json.Unmarshal([] byte(msg),& input)

switch v:= input。(type){
(Somthing1)
queueResults(v)

case Somthing2:
v = input。(Somthing2)
queueStatsRes(v)
default:
}

这个答案:

  switch v:= interface {}(input)。(type)

仍然没有成功......



有什么想法? json 打包Unmarshals的默认类型显示在

  bool,用于JSON布尔值
float64,用于JSON数字
字符串,用于JSON字符串
[] interface {},用于JSON数组
map [string]接口{},对于JSON对象
无JSON null

由于您解组为接口{} ,返回的类型将仅来自该集合。 json 包不知道 Something1 和 Something2 。您需要从json对象正在解组到的< map [string] interface {} >进行转换,或者直接解组到您想要的结构类型。



如果您不想从通用接口解包数据,或者以某种方式标记数据以便知道期望的类型,则可以迭代地使用json并尝试解组它到你想要的每种类型。



你甚至可以将它们打包到一个包装结构中,以便为你解组:

  type Something1 struct {
Thing string`json:thing`
OtherThing int64`json:other_thing`
}

type Something2 struct {
Croc int`json:croc`
Odile bool`json:odile`
}

type Unpacker struct {
Data interface {}
}

func(u * Unpacker)UnmarshalJSON(b [] byte)错误{
smth1:=& Something1 {
err:= json.Unmarshal(b,smth1)

//没有错误,bu我们还需要确保我们解组了一些
,如果err == nil&& smth1.Thing!={
u.Data = smth1
返回零
}

//如果我们有错误类型以外的错误,则中止
if _,ok:= err。(* json.UnmarshalTypeError); err!= nil&& !ok {
return err
}

smth2:=& Something2 {}
err = json.Unmarshal(b,smth2)
if err != nil {
return err
}

u.Data = smth2
return nil
}


I get a string through a rabbitmq message system. Before sending,

I use json.Marshal, convert the outcome to string and send throughrabbitmq.

The structs that I convert and send can be: (changed the names and the size of the structs but it should not matter)

type Somthing1 struct{
   Thing        string    `json:"thing"`
   OtherThing   int64     `json:"other_thing"`
}

or

type Somthing2 struct{
   Croc        int       `json:"croc"`
   Odile       bool      `json:"odile"`
}

The message goes through perfectly as a string and is printedon the other side (some server)

Up until now everything works.Now I'm trying to convert them back into their structs and assert the types.

first attempt is by:

func typeAssert(msg string) {

 var input interface{}

 json.Unmarshal([]byte(msg), &input)

 switch input.(type){
 case Somthing1:
    job := Somthing1{}
    job = input.(Somthing1)
    queueResults(job)

  case Somthing2:
    stats := Somthing2{}
    stats = input.(Somthing2)
    queueStatsRes(stats)
 default:
}

This does not work. When Printing the type of input after Unmarshalingit I get map[string]interface{} (?!?)

and even stranger than that, the map key is the string I got and the map value is empty.

I did some other attempts like:

 func typeAssert(msg string) {

  var input interface{}

  json.Unmarshal([]byte(msg), &input)

  switch v := input.(type){
  case Somthing1:
    v = input.(Somthing1)
    queueResults(v)

   case Somthing2:
    v = input.(Somthing2)
    queueStatsRes(v)
  default:
}

and also tried writing the switch like was explained in this answer:Golang: cannot type switch on non-interface value

switch v := interface{}(input).(type)

still with no success...

Any ideas?

解决方案

The default types that the json package Unmarshals into are shown in the Unmarshal function documentation

bool, for JSON booleans
float64, for JSON numbers
string, for JSON strings
[]interface{}, for JSON arrays
map[string]interface{}, for JSON objects
nil for JSON null

Since you're unmarshaling into an interface{}, the returned types will only be from that set. The json package doesn't know about Something1 and Something2. You either need to convert from the map[string]interface{} that the json object is being unmarshaled into, or unmarshal directly into the struct type you want.

If you don't want to do unpack the data from a generic interface, or somehow tag the data so you know what type to expect, you could iteratively take the json and try to unmarshal it into each type you want.

You can even pack those into a wrapper struct to do the unmarshaling for you:

type Something1 struct {
    Thing      string `json:"thing"`
    OtherThing int64  `json:"other_thing"`
}

type Something2 struct {
    Croc  int  `json:"croc"`
    Odile bool `json:"odile"`
}

type Unpacker struct {
    Data       interface{}
}

func (u *Unpacker) UnmarshalJSON(b []byte) error {
    smth1 := &Something1{}
    err := json.Unmarshal(b, smth1)

    // no error, but we also need to make sure we unmarshaled something
    if err == nil && smth1.Thing != "" {
        u.Data = smth1
        return nil
    }

    // abort if we have an error other than the wrong type
    if _, ok := err.(*json.UnmarshalTypeError); err != nil && !ok {
        return err
    }

    smth2 := &Something2{}
    err = json.Unmarshal(b, smth2)
    if err != nil {
        return err
    }

    u.Data = smth2
    return nil
}

http://play.golang.org/p/Trwd6IShDW

这篇关于解组入接口{}然后执行类型断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:43