JSON & Rest to ListBox - No Information

本文关键字:ListBox No Information to Rest amp JSON | 更新日期: 2023-09-27 18:26:02

我试图将主要元素获取到JSON提要,但它不会获取它,只会获取其他项目?为什么?这是我的代码:

    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; }
}

所以,当我做Shops.ItemsSource=myBook.SearchResponse.Phonebook.Results时,它会添加到列表框中,但会在列表框中多次显示ProjectName+Results。。。

如果我做我的Book.SearchResponse.Phonebook.Total,它没有问题。。。由于某些原因,我无法获得"结果"区域中的标题、城市等。。。为什么?

谢谢!

JSON & Rest to ListBox - No Information

根据您的描述,我相信您需要的是DataTemplate

尝试

<ListBox.ItemTemplate>
   <DataTemplate>
       <StackPanel>
           <TextBlock Text="{Binding City}" />