English 中文(简体)
尝试从第三方API获取数据,但仅获取空数组
原标题:Trying to fetch data from a third-party API but only obtaining empty arrays

我目前正在尝试自己学习一些后端,并且在尝试通过我的ASP.NET Core Web API进行第三方API调用时遇到了一些问题。我试图从TMDB API get获取电影信息,但尽管我设法以字符串的形式获取信息,但当我尝试反序列化时,它会导致空数组。

我看了几个视频,解释了进行第三方API调用的不同方法,最后使用了HttpClient方法,因为我没有建立一个类来将数据保存到中,所以我将其反序列化为一个动态对象。

namespace MoviesRouletteReactApp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MovieController : ControllerBase
    {
        [HttpGet]
        //private const string API_KEY = ;
        public async Task<IActionResult> GetMoviesList()
        {
            var movie = new List<Result>();
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://api.themoviedb.org/3/movie/now_playing?language=en-US&page=1");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "BEARER TOKEN");
                var response = await client.GetAsync(client.BaseAddress);
                if (response.IsSuccessStatusCode)
                {
                    var stringResponse = response.Content.ReadAsStringAsync().Result;
                    var dynamicResult = JsonConvert.DeserializeObject<dynamic>(stringResponse);
                    return Ok(dynamicResult.results);
                }
                else
                {
                    throw new HttpRequestException(response.ReasonPhrase);
                }
            }
        }
    }
}

这就是我在Swagger中尝试执行get请求时得到的结果:

[
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ],
  [
    [
      []
    ],
    [
      []
    ],
    [
      [
        [],
        [],
        []
      ]
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ],
    [
      []
    ]
  ]
]

我目前的知识并不丰富,而且主要是通过试错来学习,所以欢迎任何形式的反馈。

问题回答

Here var dynamicResult = JsonConvert.DeserializeObject<dynamic>(stringResponse);

不要将响应反序列化为动态,而是创建一个与API输出内联的类并反序列化为该类。

正如一些人所指出的,<code>dynamic</code>没有任何属性,所以您得到的是空类型的数组。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签