我只是不能让这种脱毒工作。它没有给出错误,但艺术家名称仍然为空。
任何人都可以帮忙吗?
json字符串:
{ "resultCount":1, "results": [ {"wrapperType":"track", "kind":"song", "artistId":414401, "collectionId":6666512, "trackId":6666508, "artistName" :"Autopilot Off", "collectionName":"Make a Sound", "trackName":"Byron Black", "collectionCensoredName":"Make a Sound", [...]"
HttpWebRequest 网络请求;
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; }
}