如何在golang中序列化为json嵌入结构

时间:2016-06-25 10:59:42

标签: json go

我刚刚开始学习Go,但我无法完成当前的任务。我需要序列化由嵌套结构组成的结构。

package main

import (
    "encoding/json"
    "fmt"
)

type Metadata struct {
    model string
}

type Texture struct {
    url      string
    hash     string
    metadata *Metadata
}

type Response struct {
    SKIN *Texture
}

func main() {
    response := Response{}
    textures := &Texture{
        url: "http://ely.by",
        hash: "123123123123123123",
    }
    metadata := &Metadata{
        model: "slim",
    }

    textures.metadata = metadata
    response.SKIN = textures

    result, _ := json.Marshal(response)
    fmt.Println(string(result))
}

始终仅输出{“SKIN”:{}}。预期价值是:

{
    "SKIN": {
        "url": "http://ely.by",
        "hash": "123123123123123123",
        "metadata": {
            "model": "slim"
        }
    }
}

我在沙箱https://play.golang.org/p/IHktK6E33N中创建了此示例。

1 个答案:

答案 0 :(得分:3)

您需要导出字段(使名称大写):

use strict;
use warnings;

open(DATA,"thiscommon.txt");
open (FILE2, '>common_element.txt') or die $!;

my %lines;
while (<DATA>) {
  print FILE2 if $lines{$_};
  print if not $lines{$_}++;
}

close DATA;
close FILE2;        

更新了游乐场示例:

https://play.golang.org/p/d-d4SJbCpH