Я просто не могу заставить эту дезерилизацию работать.Это не дает ошибки, но artistName остается пустым.
Кто-нибудь может помочь?
Json string:
{"resultCount": 1, "results": [{"wrapperType": "track", "kind": "song", "artistId": 414401, "collectionId": 6666512, "trackId": 6666508, "artistName": "Автопилот выключен", "collectionName": "Создать звук","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; }
}