本文介绍了将json反序列化为T列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi, I want deserialize the data into list GoogleImageURLs but following code returns 0 records. Please help Thanks.
[DataContract]
public class GoogleImageURL : BindableBase
{
#region Feilds
private int _width;
private int _height;
private string _imageId;
private int _tbWidth;
private int _tbHeight;
private string _unescapedUrl;
private string _url;
private string _visibleUrl;
private string _title;
private string _titleNoFormatting;
private string _originalContextUrl;
private string _content;
private string _contentNoFormatting;
private string _tbUrl;
#endregion
#region Properties
[DataMember]
public int width
{
get{return _width;}
set
{
if(_width != value)
{
_width = value;
OnPropertyChanged("width");
}
}
}
[DataMember]
public int height
{
get{return _height;}
set
{
if(_height != value)
{
_height = value;
OnPropertyChanged("height");
}
}
}
[DataMember]
public string imageId
{
get{return _imageId;}
set
{
if(_imageId != value)
{
_imageId = value;
OnPropertyChanged("imageId");
}
}
}
[DataMember]
public int tbWidth
{
get{return _tbWidth;}
set
{
if(_tbWidth != value)
{
_tbWidth = value;
OnPropertyChanged("tbWidth");
}
}
}
[DataMember]
public int tbHeight
{
get{return _tbHeight;}
set
{
if(_tbHeight != value)
{
_tbHeight = value;
OnPropertyChanged("tbHeight");
}
}
}
[DataMember]
public string unescapedUrl
{
get{return _unescapedUrl;}
set
{
if(_unescapedUrl != value)
{
_unescapedUrl = value;
OnPropertyChanged("unescapedUrl");
}
}
}
[DataMember]
public string url
{
get{return _url;}
set
{
if(_url != value)
{
_url = value;
OnPropertyChanged("url");
}
}
}
[DataMember]
public string visibleUrl
{
get{return _visibleUrl;}
set
{
if(_visibleUrl != value)
{
_visibleUrl = value;
OnPropertyChanged("visibleUrl");
}
}
}
[DataMember]
public string title
{
get{return _title;}
set
{
if(_title != value)
{
_title = value;
OnPropertyChanged("title");
}
}
}
[DataMember]
public string titleNoFormatting
{
get{return _titleNoFormatting;}
set
{
if(_titleNoFormatting != value)
{
_titleNoFormatting = value;
OnPropertyChanged("titleNoFormatting");
}
}
}
[DataMember]
public string originalContextUrl
{
get{return _originalContextUrl;}
set
{
if(_originalContextUrl != value)
{
_originalContextUrl = value;
OnPropertyChanged("originalContextUrl");
}
}
}
[DataMember]
public string content
{
get{return _content;}
set
{
if(_content != value)
{
_content = value;
OnPropertyChanged("content");
}
}
}
[DataMember]
public string contentNoFormatting
{
get{return _contentNoFormatting;}
set
{
if(_contentNoFormatting != value)
{
_contentNoFormatting = value;
OnPropertyChanged("contentNoFormatting");
}
}
}
[DataMember]
public string tbUrl
{
get { return _tbUrl; }
set
{
if (_tbUrl != value)
{
_tbUrl = value;
OnPropertyChanged("tbUrl");
}
}
}
#endregion
public static void GetData(string p_SearchString)
{
try
{
//WebClient webClient = new WebClient();//Create WebClient Object.
//string json = webClient.DownloadString("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" + p_SearchString);
WebClient wc = new WebClient();
wc.UseDefaultCredentials = true;
var data = wc.DownloadString("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" + p_SearchString);
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(data));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<GoogleImageURL>));
var result = serializer.ReadObject(ms);
List<GoogleImageURL> urls = (List<GoogleImageURL>)result;
ms.Close();
ms.Dispose();
}
catch
{
throw;
}
}
推荐答案
这篇关于将json反序列化为T列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!