将API请求拉入嵌套结构体中。

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

Pull API request into nested struct

问题

我目前正在将一个API响应解析为结构体。

对于正常的响应,我可以接受这样的格式:

[
  {"date":"2021-10-04","user":"Test","url":"Anonymous"},
  {"date":"2021-10-04","user":"Test","url":"Anonymous"},
  {"date":"2021-10-04","user":"Test","url":"Anonymous"}
]

然而,当我得到这样的数据时:

"urls": [
  {"date":"2021-10-04","user":"Test","url":"Anonymous"},
  {"date":"2021-10-04","user":"Test","url":"Anonymous"},
  {"date":"2021-10-04","user":"Test","url":"Anonymous"}
]

我无法将其解析为结构体。

这似乎是一个愚蠢的问题,因为它基本上是相同的。

以下是我的代码:

type urls struct {
    Urls []struct {
        Date string `json:"date"`
        User string `json:"user"`
        Url  string `json:"url"`
    } `json:"urls"`
}

type url []urls

func main() {
    resp, err := http.Get("https://url")
    if err != nil {
        fmt.Println("No response from request")
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    var u url
    _ = json.Unmarshal(body, &u)

    // 处理解析后的数据
}

不幸的是,这并不起作用,u 是空的。

对于第一个响应,我可以使用以下结构体,并且可以正常工作:

type urls struct {
    Date string `json:"date"`
    User string `json:"user"`
    Url  string `json:"url"`
}

希望这能帮到你!

英文:

I'm currently pulling an API response into a struct.

I'm fine with a normal response of say:

  [ 
    {"date":"2021-10-04","user":"Test","url":"Anonymous"]},
    {"date":"2021-10-04","user":"Test","url":"Anonymous"]},
    {"date":"2021-10-04","user":"Test","url":"Anonymous"]},
  ]

However when I get data like this:

  "urls": [
    {"date":"2021-10-04","user":"Test","url":"Anonymous"]},
    {"date":"2021-10-04","user":"Test","url":"Anonymous"]},
    {"date":"2021-10-04","user":"Test","url":"Anonymous"]},
  ]

I cant seem to parse it to the struct.

It seems like a stupid question as its basically the same.

Here is what I am doing:

   type urls struct {
   	Urls struct {
   		Date   string `json:"date"`
		User   string `json:"user"`
 		Urls   string `json:"urls"`
	} `json:"urls"`
   }

   type url []urls

and within the function:

   resp, err := http.Get("https://url")
   if err != nil {
		fmt.Println("No response from request")
   }
   defer resp.Body.Close()
   body, err := ioutil.ReadAll(resp.Body) // response body is []byte
   var u url
   _ = json.Unmarshal(body, &u)

Unfortunately this isnt working and u is empty.

With the first response I can have a struct like this and it works fine:

  type urls struct {
       Date   string `json:"date"`
       User   string `json:"user"`
       Urls   string `json:"urls"`
  }

答案1

得分: 0

我认为我想说的是以上内容的结合,再加上一点我的经验。

  1. 在JSON中,你的Urls字段是一个数组,但在你声明的结构体中不是。
  2. 你不应该忽略json.Unmarshal(body, &u)返回的错误。
  3. 你发布的Json不符合语法规范。
    我稍微修改了你的Json字符串,可以是这样的:
{
"urls": [
    {"date":"2021-10-04","user":"Test","url":"Anonymous"},
    {"date":"2021-10-04","user":"Test","url":"Anonymous"},
    {"date":"2021-10-04","user":"Test","url":"Anonymous"}
  ]
}

对应的Go结构体应该是:

type URL struct {
	SubURLs []struct {
		Date string `json:"date"`
		User string `json:"user"`
		URL  string `json:"url"`
	} `json:"urls"`
}

接下来,我向你介绍一种处理JSON到Go结构体的可能方法:
你可以将你的Json粘贴到这个网站上,然后你就可以得到相应的Go结构体,你也可以通过这种方式来纠正你的Json。

英文:

I think what I'm trying to say is a combination of the above and plus a little bit of my experience.

  1. Your Urls field is an array in JSON, but not in the struct you declared.
  2. You should not ignore the error returned by json.Unmarshal(body, &u) .
  3. The Json you posted is not grammatically correct.
    I modified your Json string slightly, it could be:
{
"urls": [
    {"date":"2021-10-04","user":"Test","url":"Anonymous"},
    {"date":"2021-10-04","user":"Test","url":"Anonymous"},
    {"date":"2021-10-04","user":"Test","url":"Anonymous"}
  ]
}

And to Go struct should be:

type URL struct {
	SubURLs []struct {
		Date string `json:"date"`
		User string `json:"user"`
		URL  string `json:"url"`
	} `json:"urls"`
}

Next, I introduce you to one possible way when you are dealing with JSON to Go structs:
You can paste your Json on this website, and then you can get its corresponding Go structure, and you can also correct your Json by the way.

huangapple
  • 本文由 发表于 2022年6月19日 23:45:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/72678272.html
匿名

发表评论

匿名网友

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

确定