问题
After a couple hours of research I can't find a way to transform that kind of JSON :
https://api.jamendo.com/v3.0/tracks/?client_id=56d30c95&format=jsonpretty&id=982090
{
"headers":{
"status":"success",
"code":0,
"error_message":"",
"warnings":"",
"results_count":1
},
"results":[
{
"id":"982090",
"name":"Seul",
"duration":297,
"artist_id":"350774",
"artist_name":"DON VALDES",
"artist_idstr":"DON_VALDES",
"album_name":"EVOLUTION",
"album_id":"115666",
"license_ccurl":"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/3.0\/",
"position":10,
"releasedate":"2012-11-23",
"album_image":"https:\/\/imgjam2.jamendo.com\/albums\/s115\/115666\/covers\/1.200.jpg",
"audio":"https:\/\/mp3l.jamendo.com\/?trackid=982090&format=mp31&from=app-56d30c95",
"audiodownload":"https:\/\/mp3d.jamendo.com\/download\/track\/982090\/mp32\/",
"prourl":"https:\/\/licensing.jamendo.com\/track\/982090",
"shorturl":"http:\/\/jamen.do\/t\/982090",
"shareurl":"http:\/\/www.jamendo.com\/track\/982090",
"image":"https:\/\/imgjam2.jamendo.com\/albums\/s115\/115666\/covers\/1.200.jpg"
}
]
}
Here is my method but every values in my object "track" is null and I think it's because it can't reach the "results" part of the JSON (and I can't find how to focus this part) or it's because it can't reach the first member ([0]) of "result" (and I can't a way to focus on that first element either :/) :
public static TrackModel getTrackById(int id)
{
Uri uri = new Uri(String.Format("https://api.jamendo.com/v3.0/tracks/?client_id=56d30c95&format=jsonpretty&id={0}", id));
WebRequest webRequest = WebRequest.Create(uri);
WebResponse response = webRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
String responseData = streamReader.ReadToEnd();
var track = JsonConvert.DeserializeObject<TrackModel>(responseData).;
return track;
}
I hope you will be able to help me,
Thank you very much !
回答1:
Break down your TrackModel
into two properties and have a separate class for the Result
.
public class TrackModel
{
[JsonProperty(PropertyName = "headers")]
public Headers Headers { get; set; }
[JsonProperty(PropertyName = "results")]
public Result[] Results { get; set; }
}
public class Headers
{
[JsonProperty(PropertyName = "status")]
public string Status { get; set; }
[JsonProperty(PropertyName = "code")]
public string Code { get; set; }
[JsonProperty(PropertyName = "error_message")]
public string Errormessage { get; set; }
[JsonProperty(PropertyName = "warnings")]
public string Warnings { get; set; }
[JsonProperty(PropertyName = "results_count")]
public string ResultsCount { get; set; }
}
public class Result
{
public string Id { get; set; }
public string Name { get; set; }
public int Duration { get; set; }
[JsonProperty(PropertyName = "artist_id")]
public string ArtistId { get; set; }
[JsonProperty(PropertyName = "artist_name")]
public string ArtistName { get; set; }
[JsonProperty(PropertyName = "artist_idstr")]
public string ArtistIdstr { get; set; }
[JsonProperty(PropertyName = "album_name")]
public string AlbumName { get; set; }
[JsonProperty(PropertyName = "album_id")]
public string AlbumId { get; set; }
[JsonProperty(PropertyName = "license_ccurl")]
public string LicenseCcurl { get; set; }
public int Position { get; set; }
public string Releasedate { get; set; }
[JsonProperty(PropertyName = "album_image")]
public string AlbumImage { get; set; }
public string Audio { get; set; }
public string Audiodownload { get; set; }
public string Prourl { get; set; }
public string Shorturl { get; set; }
public string Shareurl { get; set; }
public string Image { get; set; }
}
And in your original method use
var track = JsonConvert.DeserializeObject<TrackModel>(responseData).results[0].album_id;
回答2:
You try to deserialize a child object. That won't work, because the JSON serializer does not know, where TrackModel starts and where it ends. Add a "parent model" which contains a list of results. E.g.:
public class TopModel {
public ICollection<TrackModel> Results {get; set; }
}
And then use that model:
var model = JsonConvert.DeserializeObject<TrackModel>(responseData).;
Btw 1: I would use also Pascal casing in your C# files. The JSON serializer will take care of the casing-mapping.
Btw 2: Put some "using" around your streams.
回答3:
You need to create TrackModel that resembles your JSON response.
public class TrackModel
{
[JsonProperty(PropertyName="headers")]
public Headers headers {get;set;}
[JsonProperty(PropertyName="results")]
public List<Results> results {get;set;}
}
public class Headers {
public string status {get;set;}
public string code {get;set;}
public string error_message {get;set;}
//Rest of the properties
}
public class Results {
public int id {get;set;}
public string name {get;set;}
// Rest of the properties
}
Here is fiddle : https://dotnetfiddle.net/Vr27pB
回答4:
if you try to deserialize that string into the object defined by the class you wrote, it will never work. Because your TrackModel does not match the string but only the inner part of it.
Create a wrapper class like this:
public class TrackModelWrapper
{
public Headers Headers { get; set; }
public IEnumerable<TrackModel> Results { get; set; }
}
public class Headers
{
public string Status { get; set; }
public int Code { get; set; }
public string Error_message { get; set; }
public string Warnings { get; set; }
public int Results_count { get; set; }
}
public class TrackModel
{
public string id { get; set; }
public string name { get; set; }
public int duration { get; set; }
public string artist_id { get; set; }
public string artist_name { get; set; }
public string artist_idstr { get; set; }
public string album_name { get; set; }
public string album_id { get; set; }
public string license_ccurl { get; set; }
public int position { get; set; }
public string releasedate { get; set; }
public string album_image { get; set; }
public string audio { get; set; }
public string audiodownload { get; set; }
public string prourl { get; set; }
public string shorturl { get; set; }
public string shareurl { get; set; }
public string image { get; set; }
}
Then call it: var track = JsonConvert.DeserializeObject<TrackModelWrapper>(responseData);
来源:https://stackoverflow.com/questions/42914812/net-deserialize-json-from-get-api-with-header-from-c-sharp