去多级数据结构

时间:2014-08-12 04:33:52

标签: xml go

全部交易,

我想将多级数据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,你可以对它有所了解吗?

由于

1 个答案:

答案 0 :(得分:1)

以下代码可以满足您的需求。关键是将JSON消息块分成逻辑结构,然后将它们组合在一起以生成消息。有关详细信息,请参阅http://blog.golang.org/json-and-gohttp://golang.org/pkg/encoding/json/

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

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

下面的代码使用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