Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi, I want deserialize the data into list GoogleImageURLs but following code returns 0 records. Please help Thanks.



C#
[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;
           }
       }
Posted

First, test you data model. Create some instance of data (with non-zero number of records). Using DataContractJsonSerializer, write this data in some file, then read it and write to another file, compare, review with your own eyes. And compare with the JSON file downloaded from the site.

—SA
 
Share this answer
 
1. Try a HttpClient instead of WebClient. It can read and return a string and you don't have go through the trouble of using a memorystream.
2. The Json I received from this api is as follows:
{"results":[...list of GoogleImageUrls..]}
So create a type say ImageResults that has a property of a list of GoogleImageUrls. That way you can just serialize the results (if you use suggestion #1, it is far easier) to this type and access the property to get your list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900