douxi7219 2018-10-02 07:57
浏览 84
已采纳

如何简化深层JSON的封送处理

I have a JSON which contain some static and dynamic data. Below is a sample of JSON

{
  "request": {  /*Static data start */
    "data": {
      "object": { /*Static data ends here*/
        "user": { /*Dynamic data start here */
          "userid": "andmmdn",
          "ipaddr": "1.1.1.1",
          "noofusers": "100",
          "qos": "34",
          "id": "kldjflkdfjlkdjfkld",
          "domain": "xyz.com" /*Dynamic data ends here */
        } 
      }
    }
  }
}

Below is the code which can create this JSON

package main

import (
    "fmt"
    "encoding/json"
)
//ReqJSON struct
type ReqJSON struct {
    Request Request `json:"request"`
}
//Request struct
type Request struct {
    Data Data `json:"data"` 
}
//Data struct
type Data struct {
    Object Object `json:"object"`
}
//Object struct
type Object struct {
    User User `json:"user"`
}
//User struct
type User struct {
    UserID string `json:"userid"`
    IPAddr string `json:"ipaddr"`
    Noofusers string `json:"noofusers"`
    Qos string `json:"qos"`
    ID string `json:"id"`
    Domain string `json:"domain"`
}
func main() {
    test := ReqJSON {
        Request{
            Data: Data{
                Object: Object{
                    User: User{
                        UserID: "andmmdn",
                        IPAddr: "1.1.1.1",
                        Noofusers: "100",
                        Qos: "34",
                        ID: "kldjflkdfjlkdjfkld",
                        Domain: "xyz.com",
                    },  
                },  
            },
        },
    }
    jsonEncode, _ := json.Marshal(test)
    jsonIdent, _ := json.MarshalIndent(&test, "", "\t")
    fmt.Println(string(jsonEncode))
    fmt.Println(string(jsonIdent))
}

As you can see from above it contain struct which doesn't make much sense as they act more like placeholder for nesting the data. So how we make it more optimized. As all the data is being taken care in the last struct. What approach for unmarshaling of the data should be applied as the response will be in same format and want to use the last struct for the same. Any thoughts on the approach. Also how to make a generic struct as I multiple API which uses same struct below is an example

//ReqJSON for populating data
type ReqJSON struct {
    Request struct {
        Data struct {
            Object struct {
                Auth Auth `json:"auth"`
            } `json:"object"`
        } `json:"data"`
    } `json:"request"`
}
//ReqJSON for populating data
type ReqJSON struct {
    Request struct {
        Data struct {
            Object struct {
                Error Error `json:"error"`
            } `json:"object"`
        } `json:"data"`
    } `json:"request"`
}
  • 写回答

3条回答 默认 最新

  • duanqing2209 2018-10-02 08:27
    关注

    If you don't need the wrapping types for anything besides marshaling/unmarshaling, you can define them anonymously:

    type ReqJSON struct {
        Request struct {
            Data struct {
                Object struct {
                    User User `json:"user"`
                } `json:"object"`
            } `json:"data"`
        } `json:"request"`
    }
    
    type User struct {
        UserID string `json:"userid"`
        IPAddr string `json:"ipaddr"`
        Noofusers string `json:"noofusers"`
        Qos string `json:"qos"`
        ID string `json:"id"`
        Domain string `json:"domain"`
    }
    

    And, borowing from icza's answer, you can add accessor methods to ReqJSON:

    func (j *ReqJSON) User() User     { return j.Request.Data.Object.User }
    func (j *ReqJSON) SetUser(u User) { j.Request.Data.Object.User = u }
    
    func main() {
        var j ReqJSON
        j.SetUser(User{
                UserID:    "_id",
                IPAddr:    "1.1.1.1",
                Noofusers: "100",
                Qos:       "34",
                ID:        "kldjflkdfjlkdjfkld",
                Domain:    "xyz.com",
        })
    
        b, err := json.MarshalIndent(j, "", "  ")
        fmt.Println(err, string(b))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?