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!