嵌套JSON无法反序列化

时间:2018-08-20 09:51:53

标签: c# json deserialization

这是我的嵌套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

我已经生成了一个模型类,该模型类必须提供名称列表,稍后我将它们填充到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; }
    }
}

然后,在这种方法中,我尝试对数据进行反序列化,我想获取 图标,名称,ID和位置 的列表。

这是我最初拥有的方法:

 public async  void getData()
        {

            var content = await _client.GetStringAsync(URL);

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

            Debug.WriteLine("Output ", n);

        }

2 个答案:

答案 0 :(得分:2)

尝试直接反序列化到RootObject类:

JsonConvert.DeserializeObject<RootObject>(content);

然后使用LINQ检索您需要的所有数据。 声明ResultHeadInfo类,其中将包含您需要的字段:

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

然后编写LINQ查询以获取数据:

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();

顺便说一句,您只能使用Location对象而不是NorthestSouthwest,因为它们是相同的。

public class Viewport
{
    public Location northeast { get; set; }
    public Location southwest { get; set; }
}

答案 1 :(得分:0)

除了其他答案外,别忘了您可以使用属性来标识字段,但要使属性名称与通常的命名约定保持一致:

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

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

我最近为我的项目创建了这些相同的类,并使用quicktype.io来生成它们。比VS内置的常规“选择性粘贴”要好得多。

相关问题