【问题标题】:How can I get random image sources from this JSON API?如何从此 JSON API 获取随机图像源?
【发布时间】:2021-02-19 18:35:23
【问题描述】:

这是我在 StackOverflow 上的第一篇文章,所以,如果这有点不妥,请见谅。

我最近一直在使用 JSON APIs,到目前为止一直很有趣,但是,我在使用 Mars Rover API 时遇到了一些问题。

我正在尝试获取 img_src 并在我调用它们时随机获取它们。所以,我会调用该方法,它会查找所有“img_src”,然后随机选择一个。

JSON:

{
  "photos": [
    {
      "id": 102693,
      "sol": 1000,
      "camera": {
        "id": 20,
        "name": "FHAZ",
        "rover_id": 5,
        "full_name": "Front Hazard Avoidance Camera"
      },
      "img_src": "http://mars.jpl.nasa.gov/msl-raw-images/proj/msl/redops/ods/surface/sol/01000/opgs/edr/fcam/FLB_486265257EDR_F0481570FHAZ00323M_.JPG",
      "earth_date": "2015-05-30",
      "rover": {
        "id": 5,
        "name": "Curiosity",
        "landing_date": "2012-08-06",
        "launch_date": "2011-11-26",
        "status": "active"
      }
    },
    {
      "id": 102694,
      "sol": 1000,
      "camera": {
        "id": 20,
        "name": "FHAZ",
        "rover_id": 5,
        "full_name": "Front Hazard Avoidance Camera"
      },
      "img_src": "http://mars.jpl.nasa.gov/msl-raw-images/proj/msl/redops/ods/surface/sol/01000/opgs/edr/fcam/FRB_486265257EDR_F0481570FHAZ00323M_.JPG",
      "earth_date": "2015-05-30",
      "rover": {
        "id": 5,
        "name": "Curiosity",
        "landing_date": "2012-08-06",
        "launch_date": "2011-11-26",
        "status": "active"
      }
    },

上面的 JSON 是它的格式,它只是以这种格式一遍又一遍地重复(855 次)。我想要得到的只是“earth_date”和“img_src”

我的代码:

public static async Task<MarsRoverImagesModel> getMarsRoverImagesAsync()
        {
            string url = "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=1000&api_key=API_KEY";

            using (HttpResponseMessage response = await API_Stuff.APIHelper.APIClient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    // Don't know what to do here to get what I need
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }

“ApiClient”:

public class APIHelper
    {
        public static HttpClient APIClient { get; set; }
        public static void InitialiseClient()
        {
            APIClient = new HttpClient();
            APIClient.DefaultRequestHeaders.Accept.Clear();
            APIClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
    }

主要型号:

    class MarsRoverImagesModel
    {
        public string img_src { get; set; }
        public string earth_date { get; set; }
    }

然后是根模型:

    class MarsRoverRootModel
    {
        public MarsRoverImagesModel photos { get; set; }
    }

抱歉,这有点啰嗦,但我真的很感谢我能得到的任何帮助,因为这几天一直困扰着我。

谢谢!

【问题讨论】:

标签: c# json dotnet-httpclient


【解决方案1】:

首先,您的模型似乎不正确。您需要以 root 身份发布的 json 文件的类型为 photo 的列表或数组,如下所示:

    public class Camera    {
        public int id { get; set; } 
        public string name { get; set; } 
        public int rover_id { get; set; } 
        public string full_name { get; set; } 
    }

    public class Rover    {
        public int id { get; set; } 
        public string name { get; set; } 
        public string landing_date { get; set; } 
        public string launch_date { get; set; } 
        public string status { get; set; } 
    }

    public class Photo    {
        public int id { get; set; } 
        public int sol { get; set; } 
        public Camera camera { get; set; } 
        public string img_src { get; set; } 
        public string earth_date { get; set; } 
        public Rover rover { get; set; } 
    }

    public class Root    {
        public List<Photo> photos { get; set; } 
    }

然后您可以通过此调用将 json 反序列化为您的根对象:

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 

要从列表中随机选择图像,您可以使用 random。一个可能的解决方案如下:

static Random rnd = new Random();
int r = rnd.Next(Root.photos.Count);
string image = Root.photos[r].Photo.img_src;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多