2016-06-17 18 views
0

私はAPIの一般的な新機能です。私は実際に実際のプログラムを作り始めることができるので、彼らはエキサイティングです。つまり、私はYelpから結果を得る方法を考え出しました。事は、彼らがこのような形式に戻ってきて、次のとおりです。C# - Yelp API出力の書式設定

{ 
    "businesses": [ 
     { 
      "categories": [ 
       [ 
        "Local Flavor", 
        "localflavor" 
       ], 
       [ 
        "Mass Media", 
        "massmedia" 
       ] 
      ], 
      "display_phone": "+1-415-908-3801", 
      "id": "yelp-san-francisco", 
      "image_url": "http://s3-media3.fl.yelpcdn.com/bphoto/nQK-6_vZMt5n88zsAS94ew/ms.jpg", 
      "is_claimed": true, 
      "is_closed": false, 
      "location": { 
       "address": [ 
        "140 New Montgomery St" 
       ], 
       "city": "San Francisco", 
       "coordinate": { 
        "latitude": 37.7867703362929, 
        "longitude": -122.399958372115 
       }, 
       "country_code": "US", 
       "cross_streets": "Natoma St & Minna St", 
       "display_address": [ 
        "140 New Montgomery St", 
        "Financial District", 
        "San Francisco, CA 94105" 
       ], 
       "geo_accuracy": 9.5, 
       "neighborhoods": [ 
        "Financial District", 
        "SoMa" 
       ], 
       "postal_code": "94105", 
       "state_code": "CA" 
      }, 
      "mobile_url": "http://m.yelp.com/biz/yelp-san-francisco", 
      "name": "Yelp", 
      "phone": "4159083801", 
      "rating": 2.5, 
      "rating_img_url": "http://s3-media4.fl.yelpcdn.com/assets/2/www/img/c7fb9aff59f9/ico/stars/v1/stars_2_half.png", 
      "rating_img_url_large": "http://s3-media2.fl.yelpcdn.com/assets/2/www/img/d63e3add9901/ico/stars/v1/stars_large_2_half.png", 
      "rating_img_url_small": "http://s3-media4.fl.yelpcdn.com/assets/2/www/img/8e8633e5f8f0/ico/stars/v1/stars_small_2_half.png", 
      "review_count": 7140, 
      "snippet_image_url": "http://s3-media4.fl.yelpcdn.com/photo/YcjPScwVxF05kj6zt10Fxw/ms.jpg", 
      "snippet_text": "What would I do without Yelp?\n\nI wouldn't be HALF the foodie I've become it weren't for this business. \n\nYelp makes it virtually effortless to discover new...", 
      "url": "http://www.yelp.com/biz/yelp-san-francisco" 
     } 
    ], 
    "total": 2316 
} 

私は、私は、このデータアウトを分離し、WinフォームまたはWebページ上で正常に表示することに適切な形式を挿入する方法を理解していません。私が各データにアクセスする方法はないようです。私は「レスポンス」とタイプした。 IDEでJObjectを参照していますが、たとえば、boolの型はresponse.is_closedと呼ばれていません。以下は私の単純なプログラムのコードです。もちろん、これをWebアプリケーションやWinFormに変換したいのですが、このように表示されたデータは醜いので、コマンドラインウィンドウでうまくフォーマットすることもできます。ありがとうございました。

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Net; 
using System.Text; 
using CommandLine; 
using Newtonsoft.Json.Linq; 
using SimpleOAuth; 

/// <summary> 
/// Yelp API v2.0 code sample. 
/// This program demonstrates the capability of the Yelp API version 2.0 
/// by using the Search API to query for businesses by a search term and location, 
/// and the Business API to query additional information about the top result 
/// from the search query. 
/// 
/// Please refer to http://www.yelp.com/developers/documentation for the API documentation. 
/// 
/// Sample usage of the program: 
/// `YelpAPI.exe --term="bars" --location="San Francisco, CA"` 
/// </summary> 
namespace YelpAPI 
{ 
    /// <summary> 
    /// Class that encapsulates the logic for querying the API. 
    /// 
    /// Users have to set the OAuth credentials properties 
    /// in order to use this class. 
    /// </summary> 
    class YelpAPIClient 
    { 
     /// <summary> 
     /// Consumer key used for OAuth authentication. 
     /// This must be set by the user. 
     /// </summary> 
     private const string CONSUMER_KEY = "CENSORED"; 

     /// <summary> 
     /// Consumer secret used for OAuth authentication. 
     /// This must be set by the user. 
     /// </summary> 
     private const string CONSUMER_SECRET = "CENSORED"; 

     /// <summary> 
     /// Token used for OAuth authentication. 
     /// This must be set by the user. 
     /// </summary> 
     private const string TOKEN = "CENSORED"; 

     /// <summary> 
     /// Token secret used for OAuth authentication. 
     /// This must be set by the user. 
     /// </summary> 
     private const string TOKEN_SECRET = "CENSORED"; 

     /// <summary> 
     /// Host of the API. 
     /// </summary> 
     private const string API_HOST = "https://api.yelp.com"; 

     /// <summary> 
     /// Relative path for the Search API. 
     /// </summary> 
     private const string SEARCH_PATH = "/v2/search/"; 

     /// <summary> 
     /// Relative path for the Business API. 
     /// </summary> 
     private const string BUSINESS_PATH = "/v2/business/"; 

     /// <summary> 
     /// Search limit that dictates the number of businesses returned. 
     /// </summary> 
     private const int SEARCH_LIMIT = 3; 

     /// <summary> 
     /// Prepares OAuth authentication and sends the request to the API. 
     /// </summary> 
     /// <param name="baseURL">The base URL of the API.</param> 
     /// <param name="queryParams">The set of query parameters.</param> 
     /// <returns>The JSON response from the API.</returns> 
     /// <exception>Throws WebException if there is an error from the HTTP request.</exception> 
     private JObject PerformRequest(string baseURL, Dictionary<string, string> queryParams=null) 
     { 
      var query = System.Web.HttpUtility.ParseQueryString(String.Empty); 

      if (queryParams == null) 
      { 
       queryParams = new Dictionary<string, string>(); 
      } 

      foreach (var queryParam in queryParams) 
      { 
       query[queryParam.Key] = queryParam.Value; 
      } 

      var uriBuilder = new UriBuilder(baseURL); 
      uriBuilder.Query = query.ToString(); 

      var request = WebRequest.Create(uriBuilder.ToString()); 
      request.Method = "GET"; 

      request.SignRequest(
       new Tokens { 
        ConsumerKey = CONSUMER_KEY, 
        ConsumerSecret = CONSUMER_SECRET, 
        AccessToken = TOKEN, 
        AccessTokenSecret = TOKEN_SECRET 
       } 
      ).WithEncryption(EncryptionMethod.HMACSHA1).InHeader(); 

      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8); 
      return JObject.Parse(stream.ReadToEnd()); 
     } 

     /// <summary> 
     /// Query the Search API by a search term and location. 
     /// </summary> 
     /// <param name="term">The search term passed to the API.</param> 
     /// <param name="location">The search location passed to the API.</param> 
     /// <returns>The JSON response from the API.</returns> 
     public JObject Search(string term, string location) 
     { 
      string baseURL = API_HOST + SEARCH_PATH; 
      var queryParams = new Dictionary<string, string>() 
      { 
       { "term", term }, 
       { "location", location }, 
       { "limit", SEARCH_LIMIT.ToString() } 
      }; 
      return PerformRequest(baseURL, queryParams); 
     } 

     /// <summary> 
     /// Query the Business API by a business ID. 
     /// </summary> 
     /// <param name="business_id">The ID of the business to query.</param> 
     /// <returns>The JSON response from the API.</returns> 
     public JObject GetBusiness(string business_id) 
     { 
      string baseURL = API_HOST + BUSINESS_PATH + business_id; 
      return PerformRequest(baseURL); 
     } 
    } 

    /// <summary> 
    /// Command-line options abstraction. 
    /// </summary> 
    class Options 
    { 
     /// <summary> 
     /// Gets and sets the Term property. 
     /// </summary> 
     /// <value>The search term specified by the user.</value> 
     [Option('t', "term", DefaultValue="dinner", HelpText = "Search term")] 
     public string Term { get; set; } 

     /// <summary> 
     /// Gets and sets the Location property. 
     /// </summary> 
     /// <value>The location term specified by the user.</value> 
     [Option('l', "location", DefaultValue="San Francisco, CA", HelpText = "Search Location")] 
     public string Location { get; set; } 
    } 

    /// <summary> 
    /// Class that encapsulates the program entry point. 
    /// </summary> 
    class Program 
    { 
     /// <summary> 
     /// Queries the API by the input values from the user, and prints 
     /// the result on the console. 
     /// </summary> 
     /// <param name="term">The search term to query.</param> 
     /// <param name="location">The location of the business to query.</param> 
     public static void QueryAPIAndPrintResult(string term, string location) 
     { 
      var client = new YelpAPIClient(); 

      Console.WriteLine("Querying for {0} in {1}...", term, location); 

      JObject response = client.Search(term, location); 

      JArray businesses = (JArray)response.GetValue("businesses"); 

      if (businesses.Count == 0) 
      { 
       Console.WriteLine("No businesses for {0} in {1} found.", term, location); 
       return; 
      } 

      string business_id = (string)businesses[0]["id"]; 

      Console.WriteLine(
       "{0} businesses found, querying business info for the top result \"{1}\"...", 
       businesses.Count, 
       business_id 
      ); 

      response = client.GetBusiness(business_id); 

      Console.WriteLine(String.Format("Result for business {0} found:", business_id));   
      Console.WriteLine(response.ToString()); 
     } 

     /// <summary> 
     /// Program entry point. 
     /// </summary> 
     /// <param name="args">The command-line arguments.</param> 
     static void Main(string[] args) 
     { 
      bool exit = false; 
      do 
      { 
       var options = new Options(); 
       string locationInput = ""; 
       string businessTypeInput = ""; 
       try 
       { 
        CommandLine.Parser.Default.ParseArguments(args, options); 
       } 
       catch (CommandLine.ParserException) 
       { 
        Console.Error.WriteLine("Failed to parse command line options."); 
        Console.ReadKey(); 
        Environment.Exit(-1); 
       } 

       try 
       { 
        Console.WriteLine("Enter the location: "); 
        locationInput = Console.ReadLine(); 
        Console.WriteLine("Enter the type of business:"); 
        businessTypeInput = Console.ReadLine(); 
        Program.QueryAPIAndPrintResult(businessTypeInput, locationInput); 
        Console.WriteLine("Would you like to search again? Y/N?"); 
        if (Console.ReadLine().ToUpper() == "N") 
         exit = true; 
        else 
         exit = false; 
       } 
       catch (WebException error) 
       { 
        if (error.Response != null) 
         using (var reader = new StreamReader(error.Response.GetResponseStream())) 
         { 
          Console.Error.WriteLine("Response returned: {0}", reader.ReadToEnd()); 
         } 

        Console.Error.WriteLine("HTTP request failed, got {0}, abort program.", error.Message); 
       } 
      } while (exit == false); 
       Console.ReadKey(); 
       Environment.Exit(-1); 
      } 
     } 
    } 

ドキュメントはここにある:私はつかむことができるようになっていたデータのすべてを一覧表示しますhttps://www.yelp.com/developers/documentation/v2/search_api

あなたがこれを実行したい場合は、あなたがここに得ることができるのOAuth情報が必要になります:https://www.yelp.com/developers/manage_api_keys

答えて

1

YelpのAPIのいくつかの情報を探している間、私はあなたの質問を見つけたので、私はちょうどこれを自分でやりました。あなたがまだ答えを見つけていない場合は、jsonオブジェクトを解析する方法がいくつかあります。次の両方の例では、ビジネスだけを引き出し、ビジネス内のリストをループする必要があります。

//yelpJsonReturned is whatever you named your Json object returned by the API. 
//"businesses" indicates that you only want the parts of that object listed under businesses 
var businesses = yelpJsonReturned["businesses"] 

foreach (JsonValue business in businesses) 
{ 
    //parsing code goes here 
} 

1)

//parsing code goes here 
var businessList = JsonConvert.DeserializeObject<List<YelpListing>>(yourJsonString) 

これは、各リストを取り、あなたが作成したYelpListingモデルにそれをマップします。残念ながら、YelpListingモデルはGETリクエストによって返されているものと完全に一致する必要があり、APIのリターン文字列が少し畳み込まれているため型変換エラーが発生し続けていたため、これはうまくいきませんでした。また、私は実際に使用する必要のない多くの情報を持っていました。

2)yelpJsonReturned["businesses"]と同じ考えを使用して、ビジネス文字列の個々の部分を引き出すこともできます。このJsonオブジェクトの入れ子を考えると、オブジェクトを個別に引き出し、それらをすべて私のプログラムのどこかで使用できるフラットなモデルにマップするのが最も簡単だとわかりました。それは最も効率的な方法ではないかもしれませんが、私の後続モデルのコントロールが気に入っていて、YelpListingオブジェクトへのマッピングにいくつかの不満がありました。ここで私が使ったことがあります。

var yelpListingList = new List<YelpListing>(); 

var businesses = yelpJsonReturned["businesses"]; 

foreach (JsonValue business in businesses) 
{ 
    //parsing code goes here 

    var location = business["location"]; 
    var address = location["address"]; 
    var coordinates = location["coordinate"]; 

    var listing = new YelpListing() 
    { 
     Id = business["id"], 
     Name = business["name"], 
     Address = address[0], 
     City = location["city"], 
     Latitude = coordinates["latitude"], 
     Longitude = coordinates["longitude"], 
     LocationClosed = business["is_closed"], 
     MobileUrl = business["mobile_url"], 
     Rating = business["rating"], 
     NumberReviews = business["review_count"], 
     RatingImage = business["rating_img_url_small"] 
    }; 

    yelpListingList.Add(listing); 
} 
関連する問題