C# json反序列化itunes搜索api
我只是无法让这种反序列化工作发挥作用。它没有给出错误,但 ArtistName 仍为空。
有人可以帮忙吗?
Json 字符串:
{ “结果计数”:1, “结果”: [ {“wrapperType”:“track”,“kind”:“song”,“artistId”:414401,“collectionId”:6666512,“trackId”:6666508,“artistName”:“自动驾驶关闭”,“collectionName”:“Make a Sound", "trackName":"Byron Black", "collectionCensoredName":"发出声音", [...]"
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; }
}
I just cant get this deserilization to work. It gives no error but artistName remains empty.
Can anyone help?
Json string:
{
"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 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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的数据合同有问题。查看 Json 数据契约应该是这样的。
FinishWebrequest 中的 Log.xxxx 行应变为:
i think there is a problem with your data contract. Looking at the Json the Data Contract should be something like this.
and that line for Log.xxxx in FinishWebrequest should become: