Skip to main content
 首页 » 编程设计

c#之在 JSON API 调用 (CrunchBase) 中循环遍历 ListObjects>

2024年01月02日21duanxz

首先是为了让这对任何想要达到这一点的人都有帮助: 我有一个 API,我需要查询各种数据点 (CrunchBase) 我获取了 URL 并使用此工具为其生成了类 http://json2csharp.com/# (返回您需要与该 JSON URL 交互的类)接下来我需要使用该服务,输入 Scott Hanselman 进行救援,使用他的博客文章了解如何执行此操作 http://www.hanselman.com/blog/NuGetPackageOfTheWeek4DeserializingJSONWithJsonNET.aspx接下来,我将两者放在一起,并进行了一些修改,以允许我查询 CrunchBase API 以获取所需的部分信息。目前,我只是在使用控制台应用程序处理逻辑,因为我打算稍后将其包装在 .dll 中,以便在向 MVC 站点提供信息的 DataModel 项目的 DAL 层中使用。

问题:到目前为止,除了我不知道应该如何循环遍历从 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; } 
       } 
    } 
 } 

请您参考如下方法:

我猜你正在寻找这样的东西,如果不是,你应该具体说明你的问题,或者说出为什么下面的不起作用或发生了哪个错误......

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

但可以肯定的是:它可以用 C# 完成!

对于那些 List<object>属性,您应该将相应的类添加到您的模型(您确实生成的类)或尝试将这些对象转换为 JsonObject或者……像那样。 (用 VS 检查以查看实际类型。)