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"` 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?