I’m trying to get the main elements to my JSON feed but it won’t grab it, only the other items? Why? Here is my code:
public MainPage()
{
InitializeComponent();
GetData();
}
private void GetData()
{
string uri = "http://api.bing.net/json.aspx?AppId=MY-ID&Version=2.0&Market=en-US&Query=Pizza&Sources=phonebook&latitude=33.8563&longitude=-118.1232";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(uri));
request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
}
private void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
{
string resultString = streamReader1.ReadToEnd();
var ser = new DataContractJsonSerializer(typeof(RootObject));
var stream = new MemoryStream(Encoding.Unicode.GetBytes(resultString));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject));
RootObject myBook = (RootObject)jsonSerializer.ReadObject(stream);
Deployment.Current.Dispatcher.BeginInvoke(() => Shops.ItemsSource = myBook.SearchResponse.Phonebook.Results);
}
}
public class Query
{
public string SearchTerms { get; set; }
}
public class Result
{
public string Title { get; set; }
public string Url { get; set; }
public string Business { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string StateOrProvince { get; set; }
public string CountryOrRegion { get; set; }
public string PostalCode { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public string UniqueId { get; set; }
public string DisplayUrl { get; set; }
}
public class Phonebook
{
public int Total { get; set; }
public int Offset { get; set; }
public string LocalSerpUrl { get; set; }
public string Title { get; set; }
public List<Result> Results { get; set; }
}
public class SearchResponse
{
public string Version { get; set; }
public Query Query { get; set; }
public Phonebook Phonebook { get; set; }
}
public class RootObject
{
public SearchResponse SearchResponse { get; set; }
}
So, when I do Shops.ItemsSource = myBook.SearchResponse.Phonebook.Results it adds in the listbox but says ProjectName + Results several times in the listbox…
If I do myBook.SearchResponse.Phonebook.Total, it doesn’t have an issue… Some reason I can’t get it to give me the Title, City, and so on that are in the “Result” area… Why?
Thanks!
From your description, I believe what you need is DataTemplate
Try