来自外部API的json数据并返回精益的JSON数据

发布于 2025-01-17 10:43:51 字数 1731 浏览 1 评论 0 原文

我正在使用Web API练习。我的目标是创建一个从外部API接收数据的GET端点,然后返回更苗条的结果。 外部API链接: https://www.themealdb.com/api/json/v1/1/search.php?f=a , 外部API数据看起来像:

{
  "meals": [
    {
      "idMeal": "52768",
      "strMeal": "Apple Frangipan Tart",
      "strDrinkAlternate": null,
      "strCategory": "Dessert",
      .....
    },
    {
      "idMeal": "52893",
      "strMeal": "Apple & Blackberry Crumble",
      ....
     }
   ]
}

我希望我的终点提供更精细的结果:

[
    {
      "idMeal": "52768",
      "strMeal": "Apple Frangipan Tart"
    },
    {
      "idMeal": "52893",
      "strMeal": "Apple & Blackberry Crumble"
     }
 ] 

以下代码是我到目前为止尝试的,结果是无效的。我希望我不是离正确的道路太远。非常感谢

using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
using RestSharp;

namespace testAPI.Controllers;
public class Content
{
    public List<Meal> Meals { get; set; }
}

public class Meal
{
    [JsonPropertyName("idMeal")]
    public string MealId { get; set; }
    [JsonPropertyName("strMeal")]
    public string Name { get; set; }
}

[ApiController]
[Route("api/[controller]")]
public class DishesController : ControllerBase
{
    
    [HttpGet]
    public async Task<IActionResult> GetAllRecipes()
    {
        var client = new RestClient($"https://www.themealdb.com/api/json/v1/1/search.php?f=a");
        var request = new RestRequest();
        var response = await client.ExecuteAsync(request);
        var mealList = JsonSerializer.Deserialize<Content>(response.Content);
        var result = JsonSerializer.Serialize(mealList.Meals);
        
        return Ok(result);
    } 

I am practicing with web api. My goal is to create a Get endpoint, which receive data from an external api, then return a leaner result.
external api link: https://www.themealdb.com/api/json/v1/1/search.php?f=a,
The external api data looks like:

{
  "meals": [
    {
      "idMeal": "52768",
      "strMeal": "Apple Frangipan Tart",
      "strDrinkAlternate": null,
      "strCategory": "Dessert",
      .....
    },
    {
      "idMeal": "52893",
      "strMeal": "Apple & Blackberry Crumble",
      ....
     }
   ]
}

I want my endpoint provide a leaner result like the following:

[
    {
      "idMeal": "52768",
      "strMeal": "Apple Frangipan Tart"
    },
    {
      "idMeal": "52893",
      "strMeal": "Apple & Blackberry Crumble"
     }
 ] 

The following code is what I attempted so far, which result is null.. I hope I am not too far away from the right path. Thanks a lot

using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
using RestSharp;

namespace testAPI.Controllers;
public class Content
{
    public List<Meal> Meals { get; set; }
}

public class Meal
{
    [JsonPropertyName("idMeal")]
    public string MealId { get; set; }
    [JsonPropertyName("strMeal")]
    public string Name { get; set; }
}

[ApiController]
[Route("api/[controller]")]
public class DishesController : ControllerBase
{
    
    [HttpGet]
    public async Task<IActionResult> GetAllRecipes()
    {
        var client = new RestClient(
quot;https://www.themealdb.com/api/json/v1/1/search.php?f=a");
        var request = new RestRequest();
        var response = await client.ExecuteAsync(request);
        var mealList = JsonSerializer.Deserialize<Content>(response.Content);
        var result = JsonSerializer.Serialize(mealList.Meals);
        
        return Ok(result);
    } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧夏天 2025-01-24 10:43:52

我认为问题是避免化失败,因为它不知道如何进行 content.meals ,因为JSON中没有字段命名 feals - 而是称为用餐(小写 m )。

您可以通过添加 jsonpropertyname 属性来解决此问题, content class中的属性:

public class Content
{
    [JsonPropertyName("meals")]
    public List<Meal> Meals { get; set; }
}

查看用于测试运行的小提琴

I think the problem is that the deserialization fails because it doesn't know how to deserialize Content.Meals as there's no field in the JSON named Meals - instead it's called meals (lowercase m).

You could fix this by adding the JsonPropertyName attribute to the property in your Content class:

public class Content
{
    [JsonPropertyName("meals")]
    public List<Meal> Meals { get; set; }
}

Check out this fiddle for a test run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文