0

This is the link for my nested JSON : - https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=1500&type=restaurant&keyword=cruise&key=AIzaSyDk8ZfHO__XrYfDGi9RnFA_WxlVmRW5HMI

i have generated a model class which has to give me the list of names , later i populate them in a RecyclerView.

using System;
using System.Collections.Generic;

namespace newApp.Model
{

    public class Location
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }

    public class Northeast
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }

    public class Southwest
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }

    public class Viewport
    {
        public Northeast northeast { get; set; }
        public Southwest southwest { get; set; }
    }

    public class Geometry
    {
        public Location location { get; set; }
        public Viewport viewport { get; set; }
    }

    public class OpeningHours
    {
        public bool open_now { get; set; }
    }

    public class Photo
    {
        public int height { get; set; }
        public List<string> html_attributions { get; set; }
        public string photo_reference { get; set; }
        public int width { get; set; }
    }

    public class PlusCode
    {
        public string compound_code { get; set; }
        public string global_code { get; set; }
    }

    public class Result
    {
        public Geometry geometry { get; set; }
        public string icon { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public OpeningHours opening_hours { get; set; }
        public List<Photo> photos { get; set; }
        public string place_id { get; set; }
        public PlusCode plus_code { get; set; }
        public double rating { get; set; }
        public string reference { get; set; }
        public string scope { get; set; }
        public List<string> types { get; set; }
        public string vicinity { get; set; }
    }

    public class RootObject
    {
        public List<object> html_attributions { get; set; }
        public List<Result> results { get; set; }
        public string status { get; set; }
    }
}

Then , in this method i tried to deserialize the data , i want to get a list of icon, name , id and location.

This is the method i initially have :

 public async  void getData()
        {

            var content = await _client.GetStringAsync(URL);

            var n = JsonConvert.DeserializeObject<List<List<Result>>>(content);

            Debug.WriteLine("Output ", n);

        }

2 Answers 2

2

Try to deserialize to the RootObject class directly:

JsonConvert.DeserializeObject<RootObject>(content);

And then retrieve all data you need using LINQ. Declare ResultHeadInfo class which will contain fields you need:

public class ResultHeadInfo
{
    public Location Location { get; set; }
    public string Name { get; set; }
    public string Id { get; set; }
    public string Icon { get; set; }
}

Then write LINQ query to get data:

var root = JsonConvert.DeserializeObject<RootObject>(content);

List<ResultHeadInfo> infoList = root.results.Select(x => new ResultHeadInfo {
        Location = x.geometry.location,
        Name = x.name,
        Id = x.id,
        Icon = x.icon
    }).ToList();

By the way, you can use just Location object instead of Northest and Southwest as they are identical.

public class Viewport
{
    public Location northeast { get; set; }
    public Location southwest { get; set; }
}
Sign up to request clarification or add additional context in comments.

5 Comments

can you please try to add for me the linq code to search out the icon, name , id and location. It will be my pleasure
Thanks so much for your help
hey why .Select having an error , i installed the nuget package but it's showing an error
using System.Linq;
sorry my typo, results.results.Select(x => new ResultHeadInfo
0

In addition to the other answer, don't forget you can use attributes to identify the fields but keep the property names consistent with the usual naming conventions:

    public partial class Viewport
    {
        [JsonProperty("northeast")]
        public Location Northeast { get; set; }

        [JsonProperty("southwest")]
        public Location Southwest { get; set; }
    }

I created those same classes recently for my project, and used quicktype.io to generate them. Much better output than the usual 'Paste Special' built into VS.

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.