多级数据结构

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

Go multilevel data structure

问题

亲爱的所有人,

我想要将多层级的数据以 JSON 的形式返回给用户,类似于这样:

{
"screen": [
{
"screen_id": "001",
"screen_name": "screen_001",
"key": [
{
"id": "key_001",
"name": "key_001"
},
{
"id": "key_002",
"name": "key_002"
}
]
},
{
"screen_id": "002",
"screen_name": "screen_002",
"key": [
{
"id": "key_002",
"name": "key_002"
},
{
"id": "key_003",
"name": "key_003"
}
]
}
]
}

我认为使用 XML 可能更适合表示这种数据结构,因为屏幕项和键值子项将会动态插入。我是正确的吗?还是存在其他更好的方法?如果使用 XML,你能给些建议吗?

谢谢。

英文:

Deal all,

I want to return multilevel data json to user. something like this

{ "screen"[{ 
           "screen_id":"001",
           "screen_name":"screen_001"
            "key": [ {
                    "id":"key_001",
                    "name":"key_001"                                   
                    },    
                     {
                    "id":"key_002",
                    "name":"key_002"                                   
                    },   
                   ] 
           }, 
           "screen_id":"002",
           "screen_name":"screen_002"
            "key": [ {
                    "id":"key_002",
                    "name":"key_002"                                   
                    },    
                     {
                    "id":"key_003",
                    "name":"key_003"                                   
                    },   
                   ] 
       }

I guess using XML will be more suitable way to represent the data structure, because the screen item and key subitem will be inserted dynamically. Am I correct, or there exist other better way? If using XML, can u shed some light on it?

Thanks

答案1

得分: 1

以下是你要翻译的内容:

下面的代码可以实现你想要的功能。关键是将JSON消息块分成逻辑结构,然后将它们组合在一起形成消息。详细信息请参阅http://blog.golang.org/json-and-go和http://golang.org/pkg/encoding/json/。

要将对象转换为JSON,请使用json.Marshal函数

要将对象转换为XML,请使用xml.Marshal函数

下面的代码使用MarshalIndent函数对输出进行漂亮打印。

package main

import (
	"encoding/json"
	"encoding/xml"
	"fmt"
	"log"
)

type Key struct {
	Id   string `json:"id" xml:"id"`
	Name string `json:"name" xml:"name"`
}

type Screen struct {
	ScreenId   string `json:"screen_id" xml:"screen_id"`
	ScreenName string `json:"screen_name" xml:"screen_name"`
	Keys       []Key  `json:"key" xml:"key"`
}

type Message struct {
	Screens []Screen `json:"screen" xml:"screen"`
}

func main() {

	msg := Message{
		[]Screen{
			Screen{
				"001",
				"screen_001",
				[]Key{
					Key{
						"key_001",
						"key_001",
					},
					Key{
						"key_002",
						"key_002",
					},
				},
			},
			Screen{
				"002",
				"screen_002",
				[]Key{
					Key{
						"key_002",
						"key_002",
					},
					Key{
						"key_003",
						"key_003",
					},
				},
			},
		},
	}

	jsonMsg, err := json.MarshalIndent(msg, "", "\t")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", jsonMsg)

	xmlMsg, err := xml.MarshalIndent(msg, "", "\t")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", xmlMsg)
}

将产生以下输出:

{
	"screen": [
		{
			"screen_id": "001",
			"screen_name": "screen_001",
			"key": [
				{
					"id": "key_001",
					"name": "key_001"
				},
				{
					"id": "key_002",
					"name": "key_002"
				}
			]
		},
		{
			"screen_id": "002",
			"screen_name": "screen_002",
			"key": [
				{
					"id": "key_002",
					"name": "key_002"
				},
				{
					"id": "key_003",
					"name": "key_003"
				}
			]
		}
	]
}
<Message>
	<screen>
		<screen_id>001</screen_id>
		<screen_name>screen_001</screen_name>
		<key>
			<id>key_001</id>
			<name>key_001</name>
		</key>
		<key>
			<id>key_002</id>
			<name>key_002</name>
		</key>
	</screen>
	<screen>
		<screen_id>002</screen_id>
		<screen_name>screen_002</screen_name>
		<key>
			<id>key_002</id>
			<name>key_002</name>
		</key>
		<key>
			<id>key_003</id>
			<name>key_003</name>
		</key>
	</screen>
</Message>

Playground

英文:

The code below does what you want. The key is to separate the JSON message blocks into logical structs, and then group them together to make the message. See http://blog.golang.org/json-and-go and http://golang.org/pkg/encoding/json/ for more info.

To convert the object to JSON, use the json.Marshal function.

To convert the object to XML, use the xml.Marshal function.

The code below uses the MarshalIndent functions to pretty-print the output.

<!-- language: lang-go -->

package main

import (
	&quot;encoding/json&quot;
	&quot;encoding/xml&quot;
	&quot;fmt&quot;
	&quot;log&quot;
)

type Key struct {
	Id   string `json:&quot;id&quot; xml:&quot;id&quot;`
	Name string `json:&quot;name&quot; xml:&quot;name&quot;`
}

type Screen struct {
	ScreenId   string `json:&quot;screen_id&quot; xml:&quot;screen_id&quot;`
	ScreenName string `json:&quot;screen_name&quot; xml:&quot;screen_name&quot;`
	Keys       []Key  `json:&quot;key&quot; xml:&quot;key&quot;`
}

type Message struct {
	Screens []Screen `json:&quot;screen&quot; xml:&quot;screen&quot;`
}

func main() {

	msg := Message{
		[]Screen{
			Screen{
				&quot;001&quot;,
				&quot;screen_001&quot;,
				[]Key{
					Key{
						&quot;key_001&quot;,
						&quot;key_001&quot;,
					},
					Key{
						&quot;key_002&quot;,
						&quot;key_002&quot;,
					},
				},
			},
			Screen{
				&quot;002&quot;,
				&quot;screen_002&quot;,
				[]Key{
					Key{
						&quot;key_002&quot;,
						&quot;key_002&quot;,
					},
					Key{
						&quot;key_003&quot;,
						&quot;key_003&quot;,
					},
				},
			},
		},
	}

	jsonMsg, err := json.MarshalIndent(msg, &quot;&quot;, &quot;\t&quot;)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf(&quot;%s\n&quot;, jsonMsg)

	xmlMsg, err := xml.MarshalIndent(msg, &quot;&quot;, &quot;\t&quot;)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf(&quot;%s\n&quot;, xmlMsg)
}

Will produce:

{
        &quot;screen&quot;: [
                {
                        &quot;screen_id&quot;: &quot;001&quot;,
                        &quot;screen_name&quot;: &quot;screen_001&quot;,
                        &quot;key&quot;: [
                                {
                                        &quot;id&quot;: &quot;key_001&quot;,
                                        &quot;name&quot;: &quot;key_001&quot;
                                },
                                {
                                        &quot;id&quot;: &quot;key_002&quot;,
                                        &quot;name&quot;: &quot;key_002&quot;
                                }
                        ]
                },
                {
                        &quot;screen_id&quot;: &quot;002&quot;,
                        &quot;screen_name&quot;: &quot;screen_002&quot;,
                        &quot;key&quot;: [
                                {
                                        &quot;id&quot;: &quot;key_002&quot;,
                                        &quot;name&quot;: &quot;key_002&quot;
                                },
                                {
                                        &quot;id&quot;: &quot;key_003&quot;,
                                        &quot;name&quot;: &quot;key_003&quot;
                                }
                        ]
                }
        ]
}
&lt;Message&gt;
        &lt;screen&gt;
                &lt;screen_id&gt;001&lt;/screen_id&gt;
                &lt;screen_name&gt;screen_001&lt;/screen_name&gt;
                &lt;key&gt;
                        &lt;id&gt;key_001&lt;/id&gt;
                        &lt;name&gt;key_001&lt;/name&gt;
                &lt;/key&gt;
                &lt;key&gt;
                        &lt;id&gt;key_002&lt;/id&gt;
                        &lt;name&gt;key_002&lt;/name&gt;
                &lt;/key&gt;
        &lt;/screen&gt;
        &lt;screen&gt;
                &lt;screen_id&gt;002&lt;/screen_id&gt;
                &lt;screen_name&gt;screen_002&lt;/screen_name&gt;
                &lt;key&gt;
                        &lt;id&gt;key_002&lt;/id&gt;
                        &lt;name&gt;key_002&lt;/name&gt;
                &lt;/key&gt;
                &lt;key&gt;
                        &lt;id&gt;key_003&lt;/id&gt;
                        &lt;name&gt;key_003&lt;/name&gt;
                &lt;/key&gt;
        &lt;/screen&gt;
&lt;/Message&gt;

Playground

huangapple
  • 本文由 发表于 2014年8月12日 12:33:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/25256219.html
匿名

发表评论

匿名网友

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

确定