在GO语言中解析多维数组

huangapple go评论93阅读模式
英文:

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:

&lt;?php
$x=&#39;a:2:{i:0;a:6:{s:8:&quot;category&quot;;i:0;s:3:&quot;url&quot;;s:0:&quot;&quot;;s:6:&quot;ruleid&quot;;i:0;s:9:&quot;redirtype&quot;;i:0;s:8:&quot;protocol&quot;;i:0;s:6:&quot;PARSED&quot;;a:1:{s:4:&quot;path&quot;;s:0:&quot;&quot;;}}i:1;a:6:{s:8:&quot;category&quot;;i:0;s:3:&quot;url&quot;;s:3:&quot;jjj&quot;;s:6:&quot;ruleid&quot;;i:0;s:9:&quot;redirtype&quot;;i:0;s:8:&quot;protocol&quot;;i:0;s:6:&quot;PARSED&quot;;a:1:{s:4:&quot;path&quot;;s:3:&quot;jjj&quot;;}}}&#39;;
$x = unserialize($x);
//Print Array
print_r($x);
//Parse Array
foreach ($x as $key =&gt; $value){
    //commandes
    echo &quot;category is :&quot;.$x[$key][&quot;category&quot;].&quot;\n&quot;;
    
    echo &quot;path is :&quot;.$x[$key][&quot;PARSED&quot;][&quot;path&quot;].&quot;\n&quot;;
}

Output:

Array
(
    [0] =&gt; Array
        (
            [category] =&gt; 0
            
=&gt; [ruleid] =&gt; 0 [redirtype] =&gt; 0 [protocol] =&gt; 0 [PARSED] =&gt; Array ( [path] =&gt; ) ) [1] =&gt; Array ( [category] =&gt; 0
=&gt; jjj [ruleid] =&gt; 0 [redirtype] =&gt; 0 [protocol] =&gt; 0 [PARSED] =&gt; Array ( [path] =&gt; 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 (
	&quot;fmt&quot;
	&quot;github.com/techoner/gophp/serialize&quot;
)

func main() {
	var line = []byte(`a:2:{i:0;a:6:{s:8:&quot;category&quot;;i:0;s:3:&quot;url&quot;;s:0:&quot;&quot;;s:6:&quot;ruleid&quot;;i:0;s:9:&quot;redirtype&quot;;i:0;s:8:&quot;protocol&quot;;i:0;s:6:&quot;PARSED&quot;;a:1:{s:4:&quot;path&quot;;s:0:&quot;&quot;;}}i:1;a:6:{s:8:&quot;category&quot;;i:0;s:3:&quot;url&quot;;s:3:&quot;jjj&quot;;s:6:&quot;ruleid&quot;;i:0;s:9:&quot;redirtype&quot;;i:0;s:8:&quot;protocol&quot;;i:0;s:6:&quot;PARSED&quot;;a:1:{s:4:&quot;path&quot;;s:3:&quot;jjj&quot;;}}}`)
	out, _ := serialize.UnMarshal([]byte(line))
	fmt.Println(out)
}

Output:

[map[PARSED:map[path:&lt;nil&gt;] category:0 protocol:0 redirtype:0 ruleid:0 url:&lt;nil&gt;] 

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:&quot;category&quot;;i:0;s:3:&quot;url&quot;;s:0:&quot;&quot;;s:6:&quot;ruleid&quot;;i:0;s:9:&quot;redirtype&quot;;i:0;s:8:&quot;protocol&quot;;i:0;s:6:&quot;PARSED&quot;;a:1:{s:4:&quot;path&quot;;s:0:&quot;&quot;;}}i:1;a:6:{s:8:&quot;category&quot;;i:0;s:3:&quot;url&quot;;s:3:&quot;jjj&quot;;s:6:&quot;ruleid&quot;;i:0;s:9:&quot;redirtype&quot;;i:0;s:8:&quot;protocol&quot;;i:0;s:6:&quot;PARSED&quot;;a:1:{s:4:&quot;path&quot;;s:3:&quot;jjj&quot;;}}}`)

	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:&quot;category&quot;;i:0;s:3:&quot;url&quot;;s:0:&quot;&quot;;s:6:&quot;ruleid&quot;;i:0;s:9:&quot;redirtype&quot;;i:0;s:8:&quot;protocol&quot;;i:0;s:6:&quot;PARSED&quot;;a:1:{s:4:&quot;path&quot;;s:0:&quot;&quot;;}}i:1;a:6:{s:8:&quot;category&quot;;i:0;s:3:&quot;url&quot;;s:3:&quot;jjj&quot;;s:6:&quot;ruleid&quot;;i:0;s:9:&quot;redirtype&quot;;i:0;s:8:&quot;protocol&quot;;i:0;s:6:&quot;PARSED&quot;;a:1:{s:4:&quot;path&quot;;s:3:&quot;jjj&quot;;}}}`)

	out, _ := serialize.UnMarshal(line)
	dataList := out.([]interface{})
	for _, data := range dataList {
		for k, v := range data.(map[string]interface{}) {
			if k == &quot;category&quot; {
				fmt.Printf(&quot;category is :%v\n&quot;, v)
			} else if k == &quot;PARSED&quot; {
				fmt.Printf(&quot;path is :%v\n&quot;, v.(map[string]interface{})[&quot;path&quot;])
			}
		}
	}
}

output:

path is :&lt;nil&gt;
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.

huangapple
  • 本文由 发表于 2022年4月30日 07:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/72064472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定