dougu7546 2019-09-23 08:27
浏览 184

如果要在2种不同的JSON中解析对象,如何避免重复对象

I have an object that I receive:

{
    "operation": "ACC00000001", 
    "prm": "23597250350000", 
    "conso_prod": "Conso",
    "index_name": "BASE",
    "index_value": "123456",
    "timestamp": "2019-08-20T22:00:00Z"
 }

The object I use is in a common lib, so it is shared by several services:

common.Measure:

type Measure struct {
    Timestamp     time.Time
    Delta         float64
    Redistributed float64
    IsProd        bool
    IndexValue    uint32
    IndexName     string 
    Source        string 
}

and Meter:

type Meter struct {
    ID          string
    Operation string
    Unit        string
    Timestep    time.Duration
    Measures    []Measure
}

But as the labels don't match, I must create another object MeasureFromJSON which is the created based in the json I receive.

type MeasureFromJSON struct {
    Operation   string `json:"operation" binding:"required"`
    Prm         string `json:"prm"`
    Conso_prod  string `json:"conso_prod"`
    Index_name  string `json:"index_name"`
    Index_value string `json:"index_value"`
    Timestamp   string `json:"timestamp"`
}

Thing is I don't like to use 2 models for the same entity, just because I have no JSON labels in common object. Is there a way to use the common models (meter.Measure)?

  • 写回答

1条回答

  • douwen7475 2019-09-23 08:45
    关注

    Thing is I don't like to use 2 models for the same entity, just because I have no JSON labels in common object

    First of all, I have a question, how come same entity return two different sets of data that majority of the fields are different?

    If those different fields are still from single entity, then you could probably combine the fields and store it into single common.Measure.

    type Measure struct {
        Timestamp     time.Time `json:"timestamp"`
        Delta         float64
        Redistributed float64
        IsProd        bool
        IndexValue    string `json:"index_value"`
        IndexName     string `json:"index_name"` 
        Source        string 
        ConsoProd     string  `json:"conso_prod"` 
        Prm           string  `json:"prm"` 
        Operation     string  `json:"operation"` 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能