0

In C # UWP I would like to display the data from the Zomato API via Gridview, but when I run the following code there is no result. In the meantime, when I run the API in the browser, the json code works. Please help me. Thank you!

XAML code here:

<PivotItem Header="Restoranlar">
    <Grid SizeChanged="Grid_SizeChanged" Background="{ThemeResource SystemBaseLowColor}">
    <GridView x:Name="itemGridView">
        <GridView.ItemTemplate>
        <DataTemplate x:DataType="local:Restaurant">
            <Grid>
                <Image x:Name="menu" Source="{x:Bind photos_url}" HorizontalAlignment="Center" Stretch="Uniform"/>
                <StackPanel Margin="0,-25,0,0" Height="25" VerticalAlignment="Bottom">
                    <TextBlock x:Name="name" Text="{x:Bind name}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" Foreground="White" FontSize="15" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="SemiBold"/>
                </StackPanel>
                <TextBlock x:Name="ID" Text="{x:Bind url}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" Foreground="White" FontSize="17" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="SemiBold"/>
            </Grid>
        </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
    </Grid>
</PivotItem>

C# code here:

        private async void GetRestaurants()
        {
            try
            {
                string strBingImageURL = string.Format("https://developers.zomato.com/api/v2.1/search?apikey=" + APIKEY +"&count=10&lat=" + LatitudeF + "&lon=" + LongitudeF + "&category=restaurants&sort=real_distance&order=asc");
                string jsonText = "";

                HttpClient client = new HttpClient();
                HttpResponseMessage response = await client.GetAsync(new Uri(strBingImageURL));
                jsonText = await response.Content.ReadAsStringAsync();
                JsonArray jsonData1 = JsonArray.Parse(jsonText);

                foreach (JsonValue groupValue in jsonData1)
                {
                    if (jsonData1.Count > 0)
                    {
                        JsonObject groupObject = groupValue.GetObject();

                        string zname = groupObject["name"].GetString();
                        string zphotos = groupObject["photos_url"].GetString();
                        string zurl = groupObject["url"].GetString();

                        Restaurant file = new Restaurant();
                        file.name = zname;
                        file.url = zurl;
                        file.photos_url = zphotos;
                    }
                    else
                    {
                        //DoNothing
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                //Catch Here
            }
        }

Restaurant class here:

    public class Restaurant
    {
        public string name { get; set; }
        public string url { get; set; }
        public string photos_url { get; set; }
    }
10
  • I can't see from your code where you are storing the Restaurant objects, are they in an ObservableCollection or List of some sort? If that is the case you are missing your ItemsSource property on the GridView, this would explain why you see nothing. Commented Dec 31, 2017 at 15:06
  • (developers.zomato.com/api/v2.1/…) Json file of the full version is available at this link. I have nothing but these codes. I want to print some of the data in the JSON file as a Binding to the Gridview. Could you tell me what to write to the Gridview ItemsSource? Thank you for help. Commented Dec 31, 2017 at 17:45
  • any luck with the answer I provided? Commented Jan 2, 2018 at 9:41
  • Ohh I'm so sorry I couldn't write an answer, I was not interested in my code several days because of New Year. I will try this evening or tomorrow. Thank you for your interest. Commented Jan 2, 2018 at 13:31
  • Any luck with the answer provided? (If so could you please consider selecting it as the correct answer, if not I'm happy to provide further assistance) Commented Jan 10, 2018 at 20:14

1 Answer 1

2

I assume your GetRestaurants method is in the code-behind of your View or in whatever you have as the DataContext for the View.

You'll need to add a public property of type List<Restaurant> or ObservableCollection<Restaurant>. Like so:

ObservableCollection<Restaurant> Restaurants { get; set; }

Then in your foreach your new Restaurant object file to this List or ObservableCollection. I would recommend using an ObservableCollection.

Finally in your View, all you need to do is add the Restaurants property as the GridView's ItemsSource. Like so:

<GridView x:Name="itemGridView" ItemsSource="{x:Bind Restaurants}">

-Edit-

Where you are also going wrong is with the JSON deserialization. You aren't actually deserializing it in your code. You need to actually deserialize it into the objects it expects a better explanation and example of JSON deserialization can be found in this answer on StackOverflow (see below for a snippet of what I've implemented to make your code work)

HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(new Uri(zomatoURL));
jsonText = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RootObject>(jsonText);

To see a full working sample of the Zomato API and some MVVM techniques see my sample here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.