C# 嵌套的JSON无法反序列化
这是我的嵌套JSON的链接 : - 我已经生成了一个模型类,它必须给我一个名称列表,然后我在一个RecyclerView中填充它们C# 嵌套的JSON无法反序列化,c#,json,deserialization,C#,Json,Deserialization,这是我的嵌套JSON的链接 : - 我已经生成了一个模型类,它必须给我一个名称列表,然后我在一个RecyclerView中填充它们 using System; using System.Collections.Generic; namespace newApp.Model { public class Location { public double lat { get; set; } public double lng { get; set
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; }
}
}
使用系统;
使用System.Collections.Generic;
名称空间newApp.Model
{
公共类位置
{
公共双lat{get;set;}
公共双精度{get;set;}
}
公共课东北
{
公共双lat{get;set;}
公共双精度{get;set;}
}
西南公务舱
{
公共双lat{get;set;}
公共双精度{get;set;}
}
公共类视口
{
公共东北{get;set;}
公共西南西南{get;set;}
}
公共课几何
{
公共位置位置{get;set;}
公共视口{get;set;}
}
公共课开放时间
{
公共bool open_now{get;set;}
}
公开课照片
{
公共整数高度{get;set;}
公共列表html_属性{get;set;}
公共字符串photo_引用{get;set;}
公共整数宽度{get;set;}
}
公共类加码
{
公共字符串复合_代码{get;set;}
公共字符串全局_代码{get;set;}
}
公开课成绩
{
公共几何体{get;set;}
公共字符串图标{get;set;}
公共字符串id{get;set;}
公共字符串名称{get;set;}
公共开放小时开放时间{get;set;}
公开列表照片{get;set;}
公共字符串place_id{get;set;}
公共PlusCode plus_代码{get;set;}
公共双重评级{get;set;}
公共字符串引用{get;set;}
公共字符串作用域{get;set;}
公共列表类型{get;set;}
公共字符串{get;set;}
}
公共类根对象
{
公共列表html_属性{get;set;}
公共列表结果{get;set;}
公共字符串状态{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);
}
public异步void getData()
{
var content=await\u client.GetStringAsync(URL);
var n=JsonConvert.DeserializeObject(内容);
Debug.WriteLine(“输出”,n);
}
尝试直接反序列化到RootObject
类:
JsonConvert.DeserializeObject<RootObject>(content);
然后写入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();
尝试直接反序列化到
RootObject
类:
JsonConvert.DeserializeObject<RootObject>(content);
然后写入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();
除了另一个答案外,别忘了可以使用属性来标识字段,但要使属性名称与通常的命名约定保持一致:
public partial class Viewport
{
[JsonProperty("northeast")]
public Location Northeast { get; set; }
[JsonProperty("southwest")]
public Location Southwest { get; set; }
}
我最近为我的项目创建了这些类,并用来生成它们。与通常内置的“特殊粘贴”相比,输出效果要好得多。除了另一个答案之外,别忘了您可以使用属性来标识字段,但要使属性名称与通常的命名约定保持一致:
public partial class Viewport
{
[JsonProperty("northeast")]
public Location Northeast { get; set; }
[JsonProperty("southwest")]
public Location Southwest { get; set; }
}
我最近为我的项目创建了这些类,并用来生成它们。输出比VS中内置的“粘贴特殊”要好得多。请尝试为我添加linq代码以搜索图标、名称、id和位置。这将是我的荣幸非常感谢你的帮助嘿为什么。选择有一个错误,我安装了nuget软件包,但它显示了一个错误使用系统。Linq;对不起,我的输入错误,results.results.Select(x=>new ResultTheAdInfo您可以尝试为我添加linq代码以搜索图标、名称、id和位置。这将是我的荣幸!非常感谢您的帮助为什么。选择有错误,我安装了nuget软件包,但它使用System.linq显示了一个错误。对不起,我的输入错误,results.results。选择(x=>new ResultTheAdInfo