duanlang1196
duanlang1196
2017-08-16 05:49
浏览 163
已采纳

如何从golang中的接口获取值

I need to get values from a serialized string which generated from php code

So I use a package named:php_serialize to unserialize the string and then got a result of interface{} type .

But I have no idea how to get values inside the result.

This is code:

package main

import (
    "github.com/yvasiyarov/php_session_decoder/php_serialize"
    "fmt"
)

func main() {
    // this string is generated from php code
    str := `a:3:{s:4:"name";s:3:"tom";s:3:"age";s:2:"23";s:7:"friends";a:2:{i:0;a:1:{s:4:"name";s:5:"jerry";}i:1;a:1:{s:4:"name";s:4:"jack";}}}`

    decoder := php_serialize.NewUnSerializer(str)

    if result, err := decoder.Decode(); err != nil {
        panic(err)
    } else {
        fmt.Println(result)
    }
}

The print result is :

map[name:tom age:23 friends:map[0:map[name:jerry] 1:map[name:jack]]]

This result is a php_serialize.PhpValue type, which is interface{} type

The next step is how to get values inside the result?

such as get the age field and value

图片转代码服务由CSDN问答提供 功能建议

我需要从php代码生成的序列化字符串中获取值

因此,我使用一个名为: php_serialize 的包对字符串进行反序列化,然后得到 interface {} 类型的结果。

但是 我不知道如何在结果中获取值。

这是代码:

 包main 
 
import(
  “ github.com/yvasiyarov/php_session_decoder/php_serialize"
” fmt“ 
)
 
func main(){
 //此字符串由php代码生成
 str:=`a:3:{s  :4: “姓名”; S:3: “汤姆”; S:3: “年龄”,S:2: “23”; S:7: “朋友”;一个:2:{I:0;一个:  1:{S:4: “姓名”; S:5: “杰里”;} I:1;一个:1:{S:4: “姓名”; S:4: “插口”;}}}'\  n 
解码器:= php_serialize.NewUnSerializer(str)
 
如果结果为err:=解码器.Decode();  err!= nil {
 panic(err)
} else {
 fmt.Println(result)
} 
} 
   
 
 

打印结果 是:

 地图[名称:汤姆年龄:23​​朋友:地图[0:地图[名称:杰里] 1:地图[名称:杰克]]] 
   
 
 

此结果是 php_serialize.PhpValue 类型,即 interface {} 类型

下一步是如何在结果中获取值?

例如获取 age 字段和值

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douye2572
    douye2572 2017-08-16 09:17
    已采纳

    Here some ways to access the data:

    package main
    
    import (
        "fmt"
    
        "github.com/yvasiyarov/php_session_decoder/php_serialize"
    )
    
    func main() {
        // this string is generated from php code
        str := `a:3:{s:4:"name";s:3:"tom";s:3:"age";s:2:"23";s:7:"friends";a:2:{i:0;a:1:{s:4:"name";s:5:"jerry";}i:1;a:1:{s:4:"name";s:4:"jack";}}}`
    
        decoder := php_serialize.NewUnSerializer(str)
    
        result, err := decoder.Decode()
        if err != nil {
            panic(err)
        }
        fmt.Println(result)
    
        // simple assert
        t := result.(php_serialize.PhpArray)
        // use php_seriale build in function to get string
        strVal := php_serialize.PhpValueString(t["name"])
        fmt.Println(strVal)
    
        // type switch in case of different valid types
        switch t := result.(type) {
        default:
            fmt.Printf("unexpected type %T
    ", t) // %T prints whatever type t has
        case php_serialize.PhpArray:
            fmt.Println(t)
            fmt.Println(t["name"])
            fmt.Println(t["age"])
    
            // should be done recursively...
            switch f := t["friends"].(type) {
            default:
                fmt.Printf("unexpected type %T
    ", f) // %T prints whatever type t has
            case php_serialize.PhpArray:
                fmt.Println(f)
                fmt.Println(f[0])
                fmt.Println(f[1])
            }
        }
    }
    

    I hope this gives you some ideas.

    Basic concept

    • php_serialize has built in functions to convert primitives.
    • Variable structures are represented with built in types which need to be used to access the structure.
    点赞 评论
  • drq61040
    drq61040 2017-08-16 09:18

    You must assert the result to map[string]interface:

    mResult := result.(map[string]interface{})
    fmt.Println(mResult["name"])
    

    And once more assertion for friends:

    mFriends := mResult["friends"].(map[int]map[string]interface{}) 
    

    Then use it: mFriends[0]["name"]

    点赞 评论

相关推荐