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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题