gpt4 book ai didi

json - 带有 Go Parse Json 响应的 Api 请求

转载 作者:IT王子 更新时间:2023-10-29 02:33:36 25 4
gpt4 key购买 nike

编写我的第一个 Go 应用程序 我正在学习进行基本的 api 调用并解析 json 响应。我很确定我没有正确转换我的类型,我的回答是

false
0
0

0
0 false
0

如果我创建一些包含数据的数组,我可以获得该响应,但是当我将这个更复杂的 json 响应添加到混合中时,事情变得更加困惑,这让我非常肯定我没有正确转换。

这是我当前的代码,在尝试和更改内容以破坏内容并尝试解决问题之后。

package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Payload struct {
results Data
}

type Data struct {
poster_path string
adult bool
overview string
release_date string
genre_ids int
id int
original_title string
original_language string
title string
backdrop_path string
popularity float64
vote_count int
video bool
vote_average float64
}


type poster_path map[string]string
type adult map[string]bool
type overview map[string]string
type release_date map[string]string
type genre_ids map[string]int
type id map[string]int
type original_title map[string]string
type original_language map[string]string
type title map[string]string
type backdrop_path map[string]string
type popularity map[string]float64
type vote_count map[string]int
type video map[string]bool
type vote_average map[string]float64

func main() {
// http://image.tmdb.org/t/p/w185
url := "https://api.themoviedb.org/3/movie/top_rated?api_key=####APIKEYHERE######"
res, err := http.Get(url)
if err != nil {
panic(err)
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
var p Payload

err = json.Unmarshal(body, &p)
if err != nil {
panic(err)
}

fmt.Println(
p.results.poster_path, "\n", p.results.adult,
p.results.overview, "\n", p.results.release_date,
p.results.genre_ids, "\n", p.results.id,
p.results.original_title, "\n", p.results.original_language,
p.results.title, "\n", p.results.backdrop_path,
p.results.popularity, "\n", p.results.vote_count,
p.results.video, "\n", p.results.vote_average,
)
}

这是 JSON 响应的样子,

{
"page": 1,
"results": [
{
"poster_path": "/lIv1QinFqz4dlp5U4lQ6HaiskOZ.jpg",
"adult": false,
"overview": "Under the direction of a ruthless instructor, a talented young drummer begins to pursue perfection at any cost, even his humanity.",
"release_date": "2014-10-10",
"genre_ids": [
18,
10402
],
"id": 244786,
"original_title": "Whiplash",
"original_language": "en",
"title": "Whiplash",
"backdrop_path": "/6bbZ6XyvgfjhQwbplnUh1LSj1ky.jpg",
"popularity": 9.685051,
"vote_count": 1706,
"video": false,
"vote_average": 8.36
}
}

一些让我印象深刻的事情,

当我尝试转换 float 时,我对从 float32 转换为 float64 感到困惑

json 响应中有一个数组,在尝试转换时会造成混淆,

"genre_ids": [
36,
18,
53,
10752
],

最佳答案

是初学者常见的错误。由于语言设计,encoding/json 包只能解码为导出的字段

来自encoding/json包裹:

To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match. Unmarshal will only set exported fields of the struct.

要导出字段,只需使用名称的首字母大写即可。例如:

type Payload struct {
Results Data
}

代替

type Payload struct {
results Data
}

关于json - 带有 Go Parse Json 响应的 Api 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35933978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com