0

this is my first post on StackOverflow, so, sorry if this is a bit wonky.

I've recently been working with JSON Apis and has been fun thus far, but, I'm having some issues with the Mars Rover API.

I'm trying to get the img_src's and get them randomly when I call them. So, I would call the method and it would look for all of the "img_src" and then pick one at random.

The 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"
      }
    },

The JSON above is how it's formatted, and it just repeats in this format over and over (855 times). All I'm trying to get is the "earth_date" and "img_src"

My code:

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"));
        }
    }

The main model:

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

And then the root model:

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

Sorry it's a little long-winded, but I really appreciate any help I can get with this as it's been troubling me for a couple of days.

Thanks!

  • 1
    What is going wrong for you? – Crowcoder Feb 19 '21 at 18:49
  • This can be helpful: https://stackoverflow.com/q/57066632/578411 that assumes you have already access to the response body. Or maybe better: https://stackoverflow.com/questions/37642984 – rene Feb 19 '21 at 18:49

1 Answers1

0

First of all, it looks like your model is not correct. You need for the json file you posted as root a list or array of type photo like this:

    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; } 
    }

Then you could deserialize the json to your root object with this call:

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

To randomly select an image from the list you can use random. a possible solution would be the following:

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