2012-01-18 93 views
2

首先,爲了讓所有人都對此感興趣: 我有一個我需要查詢各種數據點的API(CrunchBase)使用此工具爲它生成類http://json2csharp.com/#(返回需要與該JSON URL進行交互的類)接下來,我需要使用該服務,輸入Scott Hanselman進行救援,使用他的博客文章瞭解如何執行此操作http://www.hanselman.com/blog/NuGetPackageOfTheWeek4DeserializingJSONWithJsonNET.aspx接下來我將兩人在一起,並做了一些修補以允許我查詢部分所需信息的CrunchBase API。目前我只是用控制檯應用程序來制定邏輯,因爲我打算稍後將其包裝在.dll中,以用於向MVC網站提供信息的DataModel項目的DAL層。在JSON API調用中循環訪問列表<Objects>(CrunchBase)

問題:目前爲止,除了我不知道我應該怎麼做才能遍歷從JSON端點返回的子對象。我的代碼發佈給任何想看到實現的人(我在網上找不到任何用於在C#中調用CrunchBase API的示例)此代碼在我知道需要某些有點foreach循環的位置發表評論但我發現的所有例子都不適合我。任何方向如何做到這一點非常感謝。一個例子或者一個代碼糾正對於幫助我自己和其他人自己解決其他問題將會有很大的幫助。另外的問題,這可以全部在C#中完成,或者我需要使用別的東西 - 如果有的話,你可以推薦額外的後續研究?

此代碼中使用的端點是「http://api.crunchbase.com/v/1/company/」+ CompanyName +「。js」非常感謝您的提前,我試了很久, 。

.... 
using System.Net; 
using Newtonsoft.Json; 

namespace CrunchBase 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Enter the name of a Company to look up:"); 
      string CompanyName = Console.ReadLine(); 

      var client = new WebClient(); 
      client.Headers.Add("User-Agent", "Nobody"); //my endpoint needs this... 
      var response = client.DownloadString(new Uri("http://api.crunchbase.com/v/1/company/" + CompanyName +".js")); 

      var j = JsonConvert.DeserializeObject<RootObject>(response); 
      var f = JsonConvert.DeserializeObject<FundingRound>(response); 
      var i = JsonConvert.DeserializeObject<Investment>(response); 

       Console.WriteLine("Company Name: {0}", j.name); 
       Console.WriteLine("Web Page: {0}", j.homepage_url); 
       Console.WriteLine("Email Adress: {0}", j.email_address); 
       Console.WriteLine("CruchBase Page: {0}", j.crunchbase_url); 
       Console.WriteLine("Category: {0}", j.category_code); 
       Console.WriteLine("Description: {0}", j.description); 
       Console.WriteLine("Number of Employees: {0}", j.number_of_employees); 
       Console.WriteLine("Year Founded: {0}", j.founded_year); 
       // How would the best aproach be to loop through all of the Objects and print their properties? 
       // By the way, if you run this it will hang a little bit because the FundingRound object is not 
       // properly implemented at the moment. 
       Console.WriteLine("Funding Round Type: {0}", f.round_code); 
       Console.WriteLine("Information Source: {0}", f.source_url); 
       Console.WriteLine("Description: {0}", f.source_description); 
       Console.WriteLine("Raised Amount: {0} {1}", f.raised_currency_code, f.raised_amount); 
       // I take it the same technique used to loop through the above "f" var object 
       // I would use again to loop through to the next nested Investment object 
       // and on down the chain fefore returning right back up.... 

       Console.ReadLine(); 
     } 

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

呃,很難讀。你可以縮短你的問題,只關注'我不知道該怎麼做才能遍歷從JSON端點返回的子對象。'什麼是從終點返回的?你試過什麼了? – 2012-01-18 22:10:23

回答

0

我猜你正在尋找這樣的事情,如果不是你應該specifiy你的問題,或者說,爲什麼下面不工作或發生錯誤...

foreach (var office in j.offices) 
{ 
    Console.WriteLine("Description: {0}", office.description); 
    //... 
} 

但一個當然:可以用C#完成!

對於那些List<object>屬性,您應該將相應的類添加到您的模型(您生成的類)或嘗試將這些對象轉換爲JsonObject或某物。像那樣。 (用VS查看實際類型。)

+0

這正是我想傳達的!謝謝你的幫助,直到現在纔回復給你(我是工作實習生 - 偶爾會讓我遠離pc)非常感謝!並欣賞額外的建議!你讓我今天一整天都感覺很好! – 2012-01-19 05:37:13