英文:
Parse multidimensional arrays in GO
问题
我想解析GO中的多维数组。
目标是反序列化数组字符串并运行循环以获取值。
在PHP中,我使用以下代码:
<?php
$x='a:2:{i:0;a:6:{s:8:"category";i:0;s:3:"url";s:0:"";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:0:"";}}i:1;a:6:{s:8:"category";i:0;s:3:"url";s:3:"jjj";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:3:"jjj";}}}';
$x = unserialize($x);
//Print Array
print_r($x);
//Parse Array
foreach ($x as $key => $value){
//commandes
echo "category is :".$x[$key]["category"]."\n";
echo "path is :".$x[$key]["PARSED"]["path"]."\n";
}
输出:
Array
(
[0] => Array
(
[category] => 0
=>
[ruleid] => 0
[redirtype] => 0
[protocol] => 0
[PARSED] => Array
(
[path] =>
)
)
[1] => Array
(
[category] => 0
=> jjj
[ruleid] => 0
[redirtype] => 0
[protocol] => 0
[PARSED] => Array
(
[path] => jjj
)
)
)
category is :0
path is :
category is :0
path is :jjj
在GO中,我设法反序列化字符串,但不知道如何像PHP中那样运行循环。
代码:
```go
package main
import (
"fmt"
"github.com/techoner/gophp/serialize"
)
func main() {
var line = []byte(`a:2:{i:0;a:6:{s:8:"category";i:0;s:3:"url";s:0:"";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:0:"";}}i:1;a:6:{s:8:"category";i:0;s:3:"url";s:3:"jjj";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:3:"jjj";}}}`)
out, _ := serialize.UnMarshal([]byte(line))
fmt.Println(out)
}
输出:
[map[PARSED:map[path:<nil>] category:0 protocol:0 redirtype:0 ruleid:0 url:<nil>]
map[PARSED:map[path:jjj] category:0 protocol:0 redirtype:0 ruleid:0 url:jjj]]
有人可以帮忙吗?
英文:
I wold like to parse a multidimensional array in GO
The goal is to unserialize a array string a run into a loop to get the values.
In PHP, i use this:
Code:
<?php
$x='a:2:{i:0;a:6:{s:8:"category";i:0;s:3:"url";s:0:"";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:0:"";}}i:1;a:6:{s:8:"category";i:0;s:3:"url";s:3:"jjj";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:3:"jjj";}}}';
$x = unserialize($x);
//Print Array
print_r($x);
//Parse Array
foreach ($x as $key => $value){
//commandes
echo "category is :".$x[$key]["category"]."\n";
echo "path is :".$x[$key]["PARSED"]["path"]."\n";
}
Output:
Array
(
[0] => Array
(
[category] => 0
=>
[ruleid] => 0
[redirtype] => 0
[protocol] => 0
[PARSED] => Array
(
[path] =>
)
)
[1] => Array
(
[category] => 0
=> jjj
[ruleid] => 0
[redirtype] => 0
[protocol] => 0
[PARSED] => Array
(
[path] => jjj
)
)
)
category is :0
path is :
category is :0
path is :jjj
In GO, i manage to unserialize the string but i don't know how to run the loop like in php
Code:
package main
import (
"fmt"
"github.com/techoner/gophp/serialize"
)
func main() {
var line = []byte(`a:2:{i:0;a:6:{s:8:"category";i:0;s:3:"url";s:0:"";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:0:"";}}i:1;a:6:{s:8:"category";i:0;s:3:"url";s:3:"jjj";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:3:"jjj";}}}`)
out, _ := serialize.UnMarshal([]byte(line))
fmt.Println(out)
}
Output:
[map[PARSED:map[path:<nil>] category:0 protocol:0 redirtype:0 ruleid:0 url:<nil>]
map[PARSED:map[path:jjj] category:0 protocol:0 redirtype:0 ruleid:0 url:jjj]]
Anyone can help?
答案1
得分: 0
在Golang中,没有针对未知数组或对象的解析器,你的未知数据是一个接口(interface),所以首先,你需要将数据转换为数组或对象,然后通过循环遍历它。下面的代码将实现你想要的功能。
func main() {
var line = []byte(`a:2:{i:0;a:6:{s:8:"category";i:0;s:3:"url";s:0:"";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:0:"";}}i:1;a:6:{s:8:"category";i:0;s:3:"url";s:3:"jjj";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:3:"jjj";}}}`)
out, _ := serialize.UnMarshal(line)
dataList := out.([]interface{})
for _, data := range dataList {
for k, v := range data.(map[string]interface{}) {
if k == "category" {
fmt.Printf("category is :%v\n", v)
} else if k == "PARSED" {
fmt.Printf("path is :%v\n", v.(map[string]interface{})["path"])
}
}
}
}
输出结果:
path is :<nil>
category is :0
category is :0
path is :jjj
此外,这个包没有像json.Unmarshal
那样的转换方法,所以你需要根据自己对数据的了解来进行转换。
英文:
In Golang you haven't any parser for unknown arrays or objects, your unknown data is the interface so first, you should cast the data to an array or object and then loop through it, the below code will do what you want.
func main() {
var line = []byte(`a:2:{i:0;a:6:{s:8:"category";i:0;s:3:"url";s:0:"";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:0:"";}}i:1;a:6:{s:8:"category";i:0;s:3:"url";s:3:"jjj";s:6:"ruleid";i:0;s:9:"redirtype";i:0;s:8:"protocol";i:0;s:6:"PARSED";a:1:{s:4:"path";s:3:"jjj";}}}`)
out, _ := serialize.UnMarshal(line)
dataList := out.([]interface{})
for _, data := range dataList {
for k, v := range data.(map[string]interface{}) {
if k == "category" {
fmt.Printf("category is :%v\n", v)
} else if k == "PARSED" {
fmt.Printf("path is :%v\n", v.(map[string]interface{})["path"])
}
}
}
}
output:
path is :<nil>
category is :0
category is :0
path is :jjj
also, this package hasn't any casting method like json.Unmarshal
so you should do it with your own knowledge of your data.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论