0
我只是不能得到這個工作的deserilization。它沒有錯誤,但artistName保持空白。C#json反序列化itunes搜索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; }
}