【问题标题】:Could not cast or convert from {null} to system.Int32 in JSON response C#无法在 JSON 响应 C# 中将 {null} 转换为 system.Int32
【发布时间】:2012-01-21 04:18:47
【问题描述】:

使用这段代码反序列化来自 CrunchBase 的 JSON 响应时,我遇到了以下异常。奇怪的是,它只发生在某些正在反序列化的页面上,即使结果工作正常,也没有在键:值对中具有空 []、空“”和空值的结果。如何转换或纠正我的错误?

这里抛出异常:

   JsonSerializer serializer = new JsonSerializer();
   RootObject ro = JsonConvert.DeserializeObject<RootObject>(response);

内部异常是:

   InnerException: 
   Message=Could not cast or convert from {null} to System.Int32.
   Source=Newtonsoft.Json

提前感谢您的关注!

更新: 如询问根对象的结构和该 JSON 端点上的其他对象。这些是由http://json2csharp.com/ 在将 JSON 端点的 URL 放入其中后生成的。

JSON 很长,所以这里有两个示例链接:这个可以正常工作 http://api.crunchbase.com/v/1/company/kiip.js ,而另一个(和其他)抛出异常 http://api.crunchbase.com/v/1/company/tata-communications.js

      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; }
    }

【问题讨论】:

  • 你能把你的System.Int32改成Nullable&lt;System.Int32&gt;试试看吗?
  • 你能给我们更多的信息吗? RootObject 的结构还是一些示例 JSON?

标签: c# asp.net .net json visual-studio-2010


【解决方案1】:

Json.NET 支持JSON Schema。您可以创建一个标记了所有必需属性的模式,并在反序列化之前针对它验证传入的 JSON。在此您可以检查值是否为null,您可以将其更改为某个默认值。

希望这对你有用。

【讨论】:

  • 谢谢,我会尝试一下,告诉你进展如何。感谢您的快速帮助!非常感谢!
  • 我查看了您建议的信息,不幸的是,我的理解水平使我无法掌握如何在我的应用程序中实际利用这一点。不幸的是,我认为我需要一些个人帮助才能加快速度,阅读您的个人资料,也许您或您认识的人可能会感兴趣......请看这里bangalore.craigslist.co.in/cpg/2812084605.html如果不是我的应用程序
  • @Amar Palsapure:当您有必须使用的键时,空值的合适值是什么?
猜你喜欢
  • 2019-12-28
  • 2020-03-12
  • 2018-04-30
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多