Я просто не могу заставить эту десерилизацию работать. Это не дает никаких ошибок, но имя_и_и_и_и_и остается пустым.C# json deserialization itunes search api
Может ли кто-нибудь помочь?
Json строка:
{ "resultCount": 1, "Результаты": [ { "wrapperType": "дорожка", "вид": "песня", "artistId": 414401, " collectionId ": 6666512," trackId ": 6666508," artistName ":" Autopilot Off "," collectionName ":" Make a Sound "," trackName ":" Byron Black "," collectionCensoredName ":" Make a Sound ", [ ...]»
HttpWebRequest WebRequest;
void StartWebRequest(string itunesUrl)
{
webRequest = (HttpWebRequest)WebRequest.Create(itunesUrl);
webRequest.Method = "GET";
webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
}
void FinishWebRequest(IAsyncResult result)
{
StreamReader sr = new StreamReader(webRequest.EndGetResponse(result).GetResponseStream());
string json = sr.ReadToEnd();
Log.debugToVS("json: " + json);
iTunesResult itunesObj = new iTunesResult();
itunesObj = JSONHelper.Deserialise<iTunesResult>(json);
Log.debugToVS("artistId: " + itunesObj.artistName);
}
public void iTunesSearch(string artist, string album, string title)
{
if(artist == "" && album == "" && title == "") return;
string query = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch?";
query += "term=" + HttpUtility.UrlEncode(artist + " " + album + " " + title);
query += "&media=music";
query += "&limit=20";
Log.debugToVS("url: " + query);
StartWebRequest(query);
}
}
public class JSONHelper
{
public static T Deserialise<T>(string json)
{
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms); // <== Your missing line
return obj;
}
}
[DataContract]
public class iTunesResult
{
[DataMember]
public string artistName { get; set; }
}