1 Json struct interconversion example

During a service development, the interface provided by the downstream has multiple fields, but we only need the data of some fields. For example, the downstream interface returns the following JSON data

{
    "resInfo": {"id":"65823659689"."name":"chenheng"."description":"test"."status":"success"."followLists": [43156.562158.35267]}}Copy the code

Normally we need to define the following struct to match the data in JSON

type ResInfo struct {
	Id          string `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description"`
	Status      string `json:"status"`
	FollowLists []int  `json:"followLists"`
}

type ResInfos struct {
	ResInfo ResInfo `json:"resInfo"`
}
Copy the code

Test it in the main function

var res ResInfos
b := []byte(`{ "resInfo":{ "id":"65823659689", "name":"chenheng", "description":"test", "status":"success", "FollowLists" :}} ` [43156562, 158352, 67])
json.Unmarshal(b, &res)
fmt.Println(res)
Copy the code

{{65823659689 CHENheng test success [43156 562158 35267]}}

So, going back to the first question, how do we define struct if we only want the downstream interface to return the id field, the name field in the data, and the omitEmpty tag in the JSON tag is not displayed if there are no such fields in the JSON deserialization, obviously not used here. After the experiment, we found that as long as we did not define unnecessary fields in struct, even if the interface returned all field data to you, you only defined the ID and name fields, it only parsed the data of these two fields, the code is as follows

type ResInfo struct {
	Id          string `json:"id"`
	Name        string `json:"name"`
}
Copy the code

Or deserialize the struct with the values of all the fields and see what happens

var res ResInfos
b := []byte(`{ "resInfo":{ "id":"65823659689", "name":"chenheng", "description":"test", "status":"success", "FollowLists" :}} ` [43156562, 158352, 67])
json.Unmarshal(b, &res)
fmt.Println(res)
Copy the code

The result is as follows: {{65823659689 chenheng}} perfectly solved

An empty struct does not return an empty upstream JSON string

In business development, multiple anonymous fields are combined into a JSON string returned to the upstream, but now there is a struct with empty value, do not want to return to the upstream, then what to do, for example:

type ResInfo struct {
   Id   string `json:"id"`
   Name string `json:"name"`
}

type OrderInfo struct {
   OrderId   string `json:"order_id,omitempty"`
   OrderTime string `json:"order_time,omitempty"`
}

type ResInfos struct {
   ResInfo ResInfo `json:"resInfo"`
   OrderInfo OrderInfo `json:"order_info,omitempty"`
}
Copy the code

The main function

resinfo := &ResInfos{
   ResInfo:   ResInfo{
      Id:   "645754774",
      Name: "hgfh",
   },
   OrderInfo: OrderInfo{},
}
res,_:= json.Marshal(resinfo)
fmt.Println(string(res))
Copy the code

The print result is as follows:

{"resInfo": {"id":"645754774"."name":"hgfh"},"order_info": {}}Copy the code

The resInfo structure does not initialize the OrderInfo structure, which results in deserialization to {}, but this is not friendly to upstream users. What can be done to solve this problem is to define OrderInfo as a pointer type.

type ResInfos struct {
   ResInfo ResInfo `json:"resInfo,omitempty"`
   OrderInfo *OrderInfo `json:"order_info,omitempty"`
}
Copy the code

The main function of the

resinfo := &ResInfos{
   ResInfo:   ResInfo{
      Id:   "645754774",
      Name: "hgfh",
   },
   OrderInfo: nil,
}
res,_:= json.Marshal(resinfo)
fmt.Println(string(res))
Copy the code

The print result is as follows:

{"resInfo":{"id":"645754774","name":"hgfh"}}
Copy the code

The perfect solution