C# 反序列化JSON的问题
我试图使用newtonsoft.json反序列化从White Pages Pro api返回的json字符串。在我进入商业版之前,一切都运作完美,反序列化完美无瑕。过去的一切都是我遇到问题的地方。我遇到的错误如下: WhitePagesTool.jsonPersonComplex“”不包含“Name”的定义,并且找不到接受类型为“WhitePagesTool.jsonPersonComplex”的第一个参数的扩展方法“Name”(是否缺少using指令或程序集引用?) 下面抛出的所有错误 对于以下所有情况,也会引发相同的错误: 名、中名、姓、年龄范围、性别、街道1号线、街道2号线、城市、邮政编码、州编码、国家编码、纬度、经度、准确度和相关人员 这是我的反序列化类C# 反序列化JSON的问题,c#,json,serialization,json.net,C#,Json,Serialization,Json.net,我试图使用newtonsoft.json反序列化从White Pages Pro api返回的json字符串。在我进入商业版之前,一切都运作完美,反序列化完美无瑕。过去的一切都是我遇到问题的地方。我遇到的错误如下: WhitePagesTool.jsonPersonComplex“”不包含“Name”的定义,并且找不到接受类型为“WhitePagesTool.jsonPersonComplex”的第一个参数的扩展方法“Name”(是否缺少using指令或程序集引用?) 下面抛出的所有错误 对于以
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace WhitePagesTool
{
class jsonPersonComplex
{
public string id { get; set; }
public string phone_number { get; set; }
public bool is_valid { get; set; }
public string line_type { get; set; }
public string carrier { get; set; }
public bool is_prepaid { get; set; }
public bool is_commercial { get; set; }
public List<BelongsTo> belongs_to { get; set; }
public class BelongsTo
{
public string name { get; set; }
public string firstname { get; set; }
public string middlename { get; set; }
public string lastname { get; set; }
public string age_range { get; set; }
public string gender { get; set; }
public string type { get; set; }
}
public class CurrentAddress
{
public string street_line_1 { get; set; }
public object street_line_2 { get; set; }
public string city { get; set; }
public string postal_code { get; set; }
public string state_code { get; set; }
public string country_code { get; set; }
public LatLong lat_long { get; set; }
}
public class LatLong
{
public double latitude { get; set; }
public double longitude { get; set; }
public string accuracy { get; set; }
}
public class AssociatedPeople
{
public string name { get; set; }
public string firstname { get; set; }
public string middlename { get; set; }
public string lastname { get; set; }
public string relation { get; set; }
}
}
}
您的“name”属性不在“jsonPersonComplex”类中,而是在“BelongsTo”类中
debugOutput("Name: " + jPerson.name); //should be
debugOutput("Name: " + jPerson.belongs_to().FirstOrDefault()?.name); //sorta like this for example.
此外,您的命名约定已关闭。Newtonsoft将自动为您使用大写字母,尽管小写字母应该可以使用,我建议使用c标准制作c版本。Newtonsoft将转换以下标准,这样就可以了
我试过这个,效果很好
namespace WhitePagesTool
{
class JsonPersonComplex
{
public string Id { get; set; }
public string Phone_number { get; set; }
public bool Is_valid { get; set; }
public string Line_type { get; set; }
public string Carrier { get; set; }
public bool Is_prepaid { get; set; }
public bool Is_commercial { get; set; }
public List<BelongsTo> Belongs_to { get; set; }
//Current_addresses added to address comments below.
public List<CurrentAddress> Current_addresses { get; set; }
}
public class BelongsTo
{
public string Name { get; set; }
public string Firstname { get; set; }
public string Middlename { get; set; }
public string Lastname { get; set; }
public string Age_range { get; set; }
public string Gender { get; set; }
public string Type { get; set; }
}
public class CurrentAddress
{
public string Street_line_1 { get; set; }
public object Street_line_2 { get; set; }
public string City { get; set; }
public string Postal_code { get; set; }
public string State_code { get; set; }
public string Country_code { get; set; }
public LatLong Lat_long { get; set; }
}
public class LatLong
{
public double Latitude { get; set; }
public double Longitude { get; set; }
public string Accuracy { get; set; }
}
public class AssociatedPeople
{
public string Name { get; set; }
public string Firstname { get; set; }
public string Middlename { get; set; }
public string Lastname { get; set; }
public string Relation { get; set; }
}
}
命名空间WhitePagesTool
{
类JsonPersonComplex
{
公共字符串Id{get;set;}
公用字符串电话号码{get;set;}
公共布尔值是有效的{get;set;}
公共字符串行_类型{get;set;}
公共字符串载体{get;set;}
公共bool是{get;set;}
公共布尔是商业的{get;set;}
公共列表属于{get;set;}
//当前地址添加到下面的地址注释中。
公共列表当前_地址{get;set;}
}
公共类属于
{
公共字符串名称{get;set;}
公共字符串名{get;set;}
公共字符串Middlename{get;set;}
公共字符串Lastname{get;set;}
公共字符串年龄范围{get;set;}
公共字符串{get;set;}
公共字符串类型{get;set;}
}
公共类当前地址
{
公共字符串Street_line_1{get;set;}
公共对象Street_line_2{get;set;}
公共字符串City{get;set;}
公共字符串邮政编码{get;set;}
公共字符串状态_代码{get;set;}
公共字符串国家代码{get;set;}
公共LatLong Lat_long{get;set;}
}
公共类拉特朗
{
公共双纬度{get;set;}
公共双经度{get;set;}
公共字符串精度{get;set;}
}
公共阶层协会成员
{
公共字符串名称{get;set;}
公共字符串名{get;set;}
公共字符串Middlename{get;set;}
公共字符串Lastname{get;set;}
公共字符串关系{get;set;}
}
}
然后在逻辑上
var jPerson = JsonConvert.DeserializeObject<JsonPersonComplex>(jsonString);
debugOutput("Phone ID: " + jPerson.Id.ToString());
debugOutput("Phone Number: " + jPerson.Phone_number.ToString());
debugOutput("Valid Number: " + jPerson.Is_valid);
debugOutput("Line Type: " + jPerson.Line_type);
debugOutput("Carrier: " + jPerson.Carrier);
debugOutput("Prepaid: " + jPerson.Is_prepaid);
debugOutput("Commercial: " + jPerson.Is_commercial);
debugOutput("Name: " + jPerson.Belongs_to.FirstOrDefault().Name); //and so on
//Street_line_1 added to address comments below
debugOutput("Name: " + jPerson.Belongs_to.FirstOrDefault().Street_line_1); //and so on
var jPerson=JsonConvert.DeserializeObject(jsonString);
调试输出(“电话ID:+jPerson.ID.ToString());
debugOutput(“电话号码:+jPerson.Phone_Number.ToString());
debugOutput(“有效数字:“+jPerson.Is_Valid”);
调试输出(“线型:+jPerson.Line\u型);
调试输出(“载波:+jPerson.Carrier”);
调试输出(“预付:+jPerson.Is_预付”);
调试输出(“商业:+jPerson.Is_Commercial”);
debugOutput(“名称:“+jPerson.allows_to.FirstOrDefault().Name”)//等等
//添加街道线1,以解决以下评论
debugOutput(“名称:“+jPerson.allows_to.FirstOrDefault().Street_line_1”)//等等
jsonPersonComplex
没有名称属性。所有这些名字道具都在类中。请阅读并记录。也许你的问题可以更详细一点。现在太笼统了。欢迎来到SO。请完成这次旅行。所以我的问题是正确的。审查(分类)结束。享受吧;-)非常感谢!我知道我不应该在几年前停止编码,继续使用不同的语言。一切都是那么的不同,那么VisualBasic6.0回到了我多年前编码的时候,我就可以数数了。再次感谢,这就像一个魅力,并帮助我找出另一个问题,我在以后的建设!这是完美的工作…但是,任何试图从CurrentAddress类反序列化任何内容的尝试都会抛出错误并挂起反序列化语句“我们有一个问题:值不能为null。参数名称:源”它以Street_line_1开头,尽管该值不是null。该区域中没有任何内容是空的。任何其他返回空值的区域也不会挂起,它只是将其留空。@KhaosBringer您如何尝试打印CurrentAddresses,因为记住CurrentAddresses也是Json中的一个数组。这意味着您必须在数组中查找,就像我对linq所做的那样,查找所属对象。首先,您需要将CurrentAddresses添加到原始JsonPersonComplexClasspublic List Current_addresses{get;set;}
然后你必须用bebug编写它,就像你在bebug中使用beauth todebugOutput(“Name:+jPerson.belts to.FirstOrDefault().Street_line_1”)一样//等等
@KhaosBringer请查看我在上面的评论,注意我已经更新了答案,将您的当前地址包括在评论中。您需要按照此例程完成其他对象的代码。为了节省空间,我只是简单地回答了一个简单的问题,而您添加其余部分将是一个很好的实践,有助于您理解。如果你需要更多的帮助,我很乐意帮助你。
namespace WhitePagesTool
{
class JsonPersonComplex
{
public string Id { get; set; }
public string Phone_number { get; set; }
public bool Is_valid { get; set; }
public string Line_type { get; set; }
public string Carrier { get; set; }
public bool Is_prepaid { get; set; }
public bool Is_commercial { get; set; }
public List<BelongsTo> Belongs_to { get; set; }
//Current_addresses added to address comments below.
public List<CurrentAddress> Current_addresses { get; set; }
}
public class BelongsTo
{
public string Name { get; set; }
public string Firstname { get; set; }
public string Middlename { get; set; }
public string Lastname { get; set; }
public string Age_range { get; set; }
public string Gender { get; set; }
public string Type { get; set; }
}
public class CurrentAddress
{
public string Street_line_1 { get; set; }
public object Street_line_2 { get; set; }
public string City { get; set; }
public string Postal_code { get; set; }
public string State_code { get; set; }
public string Country_code { get; set; }
public LatLong Lat_long { get; set; }
}
public class LatLong
{
public double Latitude { get; set; }
public double Longitude { get; set; }
public string Accuracy { get; set; }
}
public class AssociatedPeople
{
public string Name { get; set; }
public string Firstname { get; set; }
public string Middlename { get; set; }
public string Lastname { get; set; }
public string Relation { get; set; }
}
}
var jPerson = JsonConvert.DeserializeObject<JsonPersonComplex>(jsonString);
debugOutput("Phone ID: " + jPerson.Id.ToString());
debugOutput("Phone Number: " + jPerson.Phone_number.ToString());
debugOutput("Valid Number: " + jPerson.Is_valid);
debugOutput("Line Type: " + jPerson.Line_type);
debugOutput("Carrier: " + jPerson.Carrier);
debugOutput("Prepaid: " + jPerson.Is_prepaid);
debugOutput("Commercial: " + jPerson.Is_commercial);
debugOutput("Name: " + jPerson.Belongs_to.FirstOrDefault().Name); //and so on
//Street_line_1 added to address comments below
debugOutput("Name: " + jPerson.Belongs_to.FirstOrDefault().Street_line_1); //and so on