dongmale0656 2018-08-22 14:18 采纳率: 100%
浏览 70
已采纳

Golang中的动态JSON结构无法正常运行

I'm trying to create a struct with some basic fields which are always present and some optional fields which are structs by themselves.

I'm wondering why the following code:

package main

import (
    "encoding/json"
    "fmt"
    "time"
)

type DataManagement struct {
    DataManagement struct {
        Type              string
        Asset struct {
            LocalAssetUID string
            Type          string
        }
        *ContentProductionOrder
        *ContentItem
        TimeStamp         time.Time
        Hash              string
    }
}

type ContentProductionOrder struct {
    ProductionOrderNo int
    ItemNo            int
    StartDate         time.Time
    FinishDate        time.Time
    StatusID          int
    StatusDate        time.Time
    SourceTypeID      int
    TrackingID        int
}

type ContentItem struct {
    ItemNo     int
    ItemText   string
    Quantity   int
}

func main() {
    var contentItem ContentItem
    contentItem.ItemNo = 123
    contentItem.ItemText = "aaaaaaaa"
    contentItem.Quantity = 3

    var dataManagement DataManagement
    dataManagement.DataManagement.Type = "asd"
    dataManagement.DataManagement.Asset.LocalAssetUID = "asd"
    dataManagement.DataManagement.Asset.Type = "asd"
    dataManagement.DataManagement.ContentItem = &contentItem
    dataManagement.DataManagement.TimeStamp = time.Now().UTC()
    dataManagement.DataManagement.Hash = "123"

    xy, _ := json.MarshalIndent(dataManagement, "", "  ")
    fmt.Println(string(xy))
    xy, _ = json.MarshalIndent(contentItem, "", "  ")
    fmt.Println(string(xy))
}

outputs to the following:

{
  "DataManagement": {
    "Type": "asd",
    "Asset": {
      "LocalAssetUID": "asd",
      "Type": "asd"
    },
    "ItemText": "aaaaaaaa",
    "Quantity": 3,
    "TimeStamp": "2009-11-10T23:00:00Z",
    "Hash": "123"
  }
}
{
  "ItemNo": 123,
  "ItemText": "aaaaaaaa",
  "Quantity": 3
}

and not to:

{
  "DataManagement": {
    "Type": "asd",
    "Asset": {
      "LocalAssetUID": "asd",
      "Type": "asd"
    },
    "ContentItem": {
      "ItemNo": 123,
      "ItemText": "aaaaaaaa",
      "Quantity": 3
    },
    "TimeStamp": "2009-11-10T23:00:00Z",
    "Hash": "123"
  }
}
{
  "ItemNo": 123,
  "ItemText": "aaaaaaaa",
  "Quantity": 3
}

Any ideas? It's probably pretty easy to explain; I'm not that experienced in Golang.

Here's a Playground link: https://play.golang.org/p/iRDcaRIZ_ZU

  • 写回答

1条回答 默认 最新

  • dongshi6710 2018-08-22 14:23
    关注

    The output you are not getting which you want is because you have used embedded struct for ContentItem in DataManagement rather than field name to add to the struct.

    A field declared with a type but no explicit field name is called an embedded field. An embedded field must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.

    A field declaration will sove your issue. You should change the struct DataManagement to:

    type DataManagement struct {
        DataManagement struct {
            Type  string
            Asset struct {
                LocalAssetUID string
                Type          string
            }
            *ContentProductionOrder // this is embedded struct
            ContentItem *ContentItem
            TimeStamp time.Time
            Hash      string
        }
    }
    

    Working Code on Go Playground

    For more information, Have a look at Golang Spec for Struct Types

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题