2012-01-21 48 views
1

使用此位代碼反序列化來自CrunchBase的JSON響應時,出現以下異常。奇怪的是,它只發生在正在被反序列化的某些頁面上,即使結果工作正常,並且在鍵值對中都沒有空[],空「」和空值。我如何施放或糾正我的錯誤?無法在JSON響應中從{null}轉換或轉換爲system.Int32 C#

異常被拋出在這裏:

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端點上的根對象和其他對象的結構。在將JSON端點的URL放入其中之後,這些內容由http://json2csharp.com/生成。

的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; } 
    } 
+0

你可以改變你的'System.Int32'爲'Nullable '並嘗試? –

+0

你能給我們更多的信息嗎? RootObject的結構或者一些示例JSON? – JaredPar

回答

0

Json.NET支持JSON Schema。您可以創建一個具有所有必需屬性的模式,並在反序列化之前驗證傳入的JSON。在這裏你可以檢查是否值是null你可以改變它爲某個默認值。

希望這對你有用。

+0

謝謝,我會試着知道,並讓你知道它是如何去的。感謝您的快速幫助!非常感激! –

+0

我查看了你所建議的信息,不幸的是,我的理解程度讓我無法掌握如何在應用程序中真正利用這一點。不幸的是,我認爲我需要一點點個人幫助,以加快速度,閱讀你的個人資料,也許你或你認識的人可能感興趣......請看這裏http://bangalore.craigslist.co.in/cpg/ 2812084605.html如果不是我的appologies –

+0

@Amar Palsapure:當您必須使用密鑰時,什麼是空值的適當值? – Phil

相關問題