Returning an array of structure as Json Response using GO

huangapple go评论64阅读模式
英文:

Returning an array of structure as Json Response using GO

问题

我正在使用GO构建一个REST API,并且能够从服务器获取JSON响应。我希望将JSON响应存储在某种容器(数组)中,并从函数中返回该结构。我定义了如下的数据结构:

type Payload struct {
    Stuff []Data `json:"data"`  // 保存返回的JSON响应
}

type Container struct {
    container []Payload
}

type ListContainersResponse struct {
    Data []Container    // 希望从函数中返回这个结构
}

func (client *Client) ListContainers() (ListContainersResponse, error) {
    // 获取JSON响应
    var p Payload

    // XYZ是需要返回的ListContainersResponse类型
    return XYZ
}

遍历p可以得到JSON结构,我想将其附加到一个Data[]容器中,该容器可以保存返回的JSON响应并从函数中返回。我尝试过一些操作,但是遇到了一些异常。有人可以帮助我吗?

谢谢,我通过以下方式使我的代码工作起来了:

var result ListContainersResponse
var temp Container
temp.container = append(temp.container, p)
result.Data = append(result.Data, temp)
英文:

I am building a REST api in GO and I am able to fetch the JSON response from the server. I am looking forward to store the JSON response in sort of a container (array) and return that structure from the function. I have my data structures defined something like this -
{

type Payload struct {
	Stuff []Data `json:"data"`  // holds the JSON response returned
}

type Container struct {
	container []Payload
}

type ListContainersResponse struct {
	Data []Container    // want this thing to be returned from the function
}


func (client *Client) ListContainers() (ListContainersResponse, error) {
   // fetches the JSON response 
   var p Payload 

    // XYZ is something of the type ListContainersResponse which needs to be returned 
   return  XYZ
}

}

iterating over p gives me my JSON structure and I want to append it to a Data[] container which can hold this returned JSON response and returned back from the function. I tried playing around with it but get some exceptions. Could someone help me with this?

Thanks, I got my code working by doing something like this
{

var result ListContainersResponse
	var temp Container
	temp.container = append(temp.container, p)
	result.Data = append(result.Data, temp)

}

答案1

得分: 0

假设您已经将JSON解码为Payload p的实例。

func (client *Client) ListContainers() (ListContainersResponse, error) {
   // 获取JSON响应
   var p Payload 

   XYZ := ListContainersResponse {
       Data: []Container {
           container: []Payload {
               {p},
           },
       },
   }

   // XYZ是ListContainersResponse类型的对象,需要返回
   return XYZ
}

此外,如果有其他人感兴趣,以下是将JSON转换为结构体的方法:

var p Payload
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&p)

其中,r的类型为http.Request
我期望JSON字符串的格式如下:

{
    "data": [...]
}
英文:

Assuming you've already got the JSON decoded into an instance of Payload p.

func (client *Client) ListContainers() (ListContainersResponse, error) {
   // fetches the JSON response 
   var p Payload 

   XYZ := ListContainersResponse {
       Data: []Container {
           container: []Payload {
               {p},
           },
       },
   }

   // XYZ is something of the type ListContainersResponse which needs to be returned 
   return XYZ
}

Additionally, in case any else is interested, here is how I would get the JSON into the struct:

var p Payload
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&p)

Where r is of type http.Request.
I expect the json string looks something like:

{
    "data": [...]
}

huangapple
  • 本文由 发表于 2014年11月19日 06:00:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/27004910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定