I am trying to access this api the documentation quite good but I am having trouble deserlizating the object.
You can you my test url here will change api key after.
https://services.marinetraffic.com/api/vesselmasterdata/v:3/APiKey/imo:9661792/protocol:jsono
This is the object class.
public partial class MaraineApiData {
[JsonProperty("METADATA")]
public Metadata Metadata { get; set; }
[JsonProperty("DATA")]
public List<Datum> Data { get; set; }
}
public partial class Datum {
[JsonProperty("MMSI")]
public long Mmsi { get; set; }
[JsonProperty("IMO")]
public long Imo { get; set; }
[JsonProperty("NAME")]
public string Name { get; set; }
[JsonProperty("PLACE_OF_BUILD")]
public string PlaceOfBuild { get; set; }
[JsonProperty("BUILD")]
public long Build { get; set; }
[JsonProperty("BREADTH_EXTREME")]
public long BreadthExtreme { get; set; }
[JsonProperty("SUMMER_DWT")]
public long SummerDwt { get; set; }
[JsonProperty("DISPLACEMENT_SUMMER")]
public string DisplacementSummer { get; set; }
[JsonProperty("CALLSIGN")]
public string Callsign { get; set; }
[JsonProperty("FLAG")]
public string Flag { get; set; }
[JsonProperty("DRAUGHT")]
public string Draught { get; set; }
[JsonProperty("LENGTH_OVERALL")]
public long LengthOverall { get; set; }
[JsonProperty("FUEL_CONSUMPTION")]
public string FuelConsumption { get; set; }
[JsonProperty("SPEED_MAX")]
public string SpeedMax { get; set; }
[JsonProperty("SPEED_SERVICE")]
public string SpeedService { get; set; }
[JsonProperty("LIQUID_OIL")]
public string LiquidOil { get; set; }
[JsonProperty("OWNER")]
public string Owner { get; set; }
[JsonProperty("MANAGER")]
public string Manager { get; set; }
[JsonProperty("MANAGER_OWNER")]
public string ManagerOwner { get; set; }
[JsonProperty("VESSEL_TYPE")]
public string VesselType { get; set; }
}
public partial class Metadata {
[JsonProperty("TOTAL_RESULTS")]
public long TotalResults { get; set; }
[JsonProperty("TOTAL_PAGES")]
public long TotalPages { get; set; }
[JsonProperty("CURRENT_PAGE")]
public long CurrentPage { get; set; }
}
But this is how I am using to get my data from the api.
public async Task<List<Metadata>> GetMarineApiData(string IMOCode) {
string VechnicleDataURL = $"vesselmasterdata/v:3/{ApiKey}/imo:{IMOCode}/protocol:jsono";
List<Metadata> _result = new List<Metadata>();
var uri = new Uri(string.Format(APIUrl + VechnicleDataURL, string.Empty));
var response = await _client.GetAsync(uri);
if (response.IsSuccessStatusCode) {
var byteArray = await response.Content.ReadAsByteArrayAsync();
var content = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
_result = JsonConvert.DeserializeObject<List<Metadata>>(content);
}
return _result.ToList();
}
Error
O> Cannot deserialize the current JSON object (eg {"name":"value"})
into type 'System.Collections.Generic.List`1[MISSystem.Dal.Metadata]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (
The api string you have provided returns not an array but an object:
{
"METADATA": { ...},
"DATA": [{...}]
}
Try to replace
JsonConvert.DeserializeObject<List<Metadata>>(content);
with
JsonConvert.DeserializeObject<MaraineApiData >(content);
and it should be fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.