C# 无法在JSON响应C中从{null}强制转换或转换为system.Int32
当使用这段代码从CrunchBase反序列化JSON响应时,我遇到以下异常。奇怪的是,这种情况只发生在某些正在进行反序列化的页面上,即使结果正常工作,或者在key:value对中都没有空[]、空和空值。我怎样才能纠正我的错误 在此处引发异常:C# 无法在JSON响应C中从{null}强制转换或转换为system.Int32,c#,asp.net,.net,json,visual-studio-2010,C#,Asp.net,.net,Json,Visual Studio 2010,当使用这段代码从CrunchBase反序列化JSON响应时,我遇到以下异常。奇怪的是,这种情况只发生在某些正在进行反序列化的页面上,即使结果正常工作,或者在key:value对中都没有空[]、空和空值。我怎样才能纠正我的错误 在此处引发异常: JsonSerializer serializer = new JsonSerializer(); RootObject ro = JsonConvert.DeserializeObject<RootObject>(response
JsonSerializer serializer = new JsonSerializer();
RootObject ro = JsonConvert.DeserializeObject<RootObject>(response);
提前谢谢你的眼睛
更新:
询问根对象的结构以及JSON端点上的其他对象。这些是在将JSON端点的URL放入其中后生成的
JSON很长,因此这里有两个示例链接:这个链接工作正常,而另一个和其他链接抛出异常
Json.NET支持。您可以创建一个带有标记的所有必需属性的模式,并在反序列化之前验证传入的JSON。在这种情况下,您可以检查值是否为null,您可以将其更改为某个默认值
希望这对您有用。您能将System.Int32更改为Nullable并重试吗?您能提供更多信息吗?RootObject的结构或者一些示例JSON?谢谢,我将尝试了解并让您知道它是如何运行的。谢谢你的快速帮助!非常感谢!我查看了您建议的信息,不幸的是,我的理解水平使我无法掌握如何在我的应用程序中实际利用这些信息。信息方面,我想我需要一些个人的帮助来加快速度,阅读你的个人资料,也许你或者你认识的人可能会感兴趣。。。如果不是我的,请看这里appologies@AmarPALAPURE:当您有必须使用的键时,空值的合适值是什么?
InnerException:
Message=Could not cast or convert from {null} to System.Int32.
Source=Newtonsoft.Json
public class Image
{
public List<List<object>> available_sizes { get; set; }
public object attribution { get; set; }
}
public class Person
{
public string first_name { get; set; }
public string last_name { get; set; }
public string permalink { get; set; }
}
public class Relationship
{
public bool is_past { get; set; }
public string title { get; set; }
public Person person { get; set; }
}
public class Provider
{
public string name { get; set; }
public string permalink { get; set; }
}
public class Providership
{
public string title { get; set; }
public bool is_past { get; set; }
public Provider provider { get; set; }
}
public class FinancialOrg
{
public string name { get; set; }
public string permalink { get; set; }
}
public class Person2
{
public string first_name { get; set; }
public string last_name { get; set; }
public string permalink { get; set; }
}
public class Investment
{
public object company { get; set; }
public FinancialOrg financial_org { get; set; }
public Person2 person { get; set; }
}
public class FundingRound
{
public string round_code { get; set; }
public string source_url { get; set; }
public string source_description { get; set; }
public double raised_amount { get; set; }
public string raised_currency_code { get; set; }
public int funded_year { get; set; }
public int funded_month { get; set; }
public int funded_day { get; set; }
public List<Investment> investments { get; set; }
}
public class Office
{
public string description { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string zip_code { get; set; }
public string city { get; set; }
public string state_code { get; set; }
public string country_code { get; set; }
public object latitude { get; set; }
public object longitude { get; set; }
}
public class VideoEmbed
{
public string embed_code { get; set; }
public string description { get; set; }
}
public class Screenshot
{
public List<List<object>> available_sizes { get; set; }
public object attribution { get; set; }
}
public class RootObject
{
public string name { get; set; }
public string permalink { get; set; }
public string crunchbase_url { get; set; }
public string homepage_url { get; set; }
public string blog_url { get; set; }
public string blog_feed_url { get; set; }
public string twitter_username { get; set; }
public string category_code { get; set; }
public int number_of_employees { get; set; }
public int founded_year { get; set; }
public int founded_month { get; set; }
public object founded_day { get; set; }
public object deadpooled_year { get; set; }
public object deadpooled_month { get; set; }
public object deadpooled_day { get; set; }
public object deadpooled_url { get; set; }
public string tag_list { get; set; }
public string alias_list { get; set; }
public string email_address { get; set; }
public string phone_number { get; set; }
public string description { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string overview { get; set; }
public Image image { get; set; }
public List<object> products { get; set; }
public List<Relationship> relationships { get; set; }
public List<object> competitions { get; set; }
public List<Providership> providerships { get; set; }
public string total_money_raised { get; set; }
public List<FundingRound> funding_rounds { get; set; }
public List<object> investments { get; set; }
public object acquisition { get; set; }
public List<object> acquisitions { get; set; }
public List<Office> offices { get; set; }
public List<object> milestones { get; set; }
public object ipo { get; set; }
public List<VideoEmbed> video_embeds { get; set; }
public List<Screenshot> screenshots { get; set; }
public List<object> external_links { get; set; }
}