英文:
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>
英文:
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 (
	"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)
}
Will produce:
{
        "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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论