2014-03-14 86 views
0

我正在使用google api,爲了能夠獲取我想要的值,我需要一些轉換。Google API - 向屬性添加API值

當發送查詢請求到網上搜尋API它返回一個字符串列表,像這樣:

//Request to Google API 
    Google.Apis.Analytics.v3.Data.GaData d = r.Execute(); 

//The property in Gadata used to query Google data 
    public virtual IList<IList<string>> Rows { get; set; } 

列表中的「行」返回兩個值,日期和遊客。但是,這兩個值不是屬性,結果只是索引器。例如,如果我寫:

//Creating a list where to add dates 
List<string> ListVisitors = new List<string>(); 

//iterates date and visitors and place it into the list 
     foreach (var row in d.Rows) 
     { 
      ListVisitors.Add(row[0]); 
      ListVisitors.Add(row[1]); 

      Console.WriteLine("Date:" + " " + row[0] + " " + "Visitors:" + " " + row[1]); 
      Console.ReadLine(); 
     } 

正如你所期望的結果是這樣的:

enter image description here

enter image description here

現在這裏是問題。我想將這些數據(日期和訪問者)用於其他Google API。問題是爲了做到這一點,我需要日期和遊客成爲屬性。

如果我做一個清單這樣會工作:

類:

public class GAStatistics 
{ 
    public string Dates { get; set; } 
    public string Visitors { get; set; } 
} 

主營:

static void Main(string[] args) 
     { 

      List<GAStatistics> ListDates = new List<GAStatistics>(); 

      GAStatistics Date1 = new GAStatistics() { Dates = "20140228", Visitors = "871"}; 
      GAStatistics Date2 = new GAStatistics() { Dates = "20140227", Visitors = "593" }; 
      GAStatistics Date3 = new GAStatistics() { Dates = "20140226", Visitors = "553" }; 

      ListDates.Add(Date1); 
      ListDates.Add(Date2); 
      ListDates.Add(Date3); 

      var Exp = string.Join(Environment.NewLine, ListDates.Select(e => string.Format("{0}", e.Dates + "" + "-" + " " + e.Visitors)).ToArray()); 
      Console.WriteLine(Exp); 
      Console.ReadLine(); 
     } 
    } 

這裏我有,我可以使用其他谷歌兩個屬性API:

enter image description here

問題是:我可以使用date和visitor的屬性創建一個類,並將它們聲明爲來自行的值嗎?

謝謝!

/Chris

回答

0

問題解決!

類別:

public class GAStatistics 
    { 

     public string Date { get; set; } 
     public string Visitors { get; set; } 


     public GAStatistics(string _date, string _visitors) 
     { 

      Date = _date; 
      Visitors = _visitors; 

     } 



    } 

主程序:

增量通過在谷歌分析API從各行用數據填充ListGaVisitors GaVisits

List<GAStatistics> ListGaVisitors = new List<GAStatistics>(); 

    foreach (var row in d.Rows) 
    { 

    GAStatistics GaVisits = new GAStatistics(row[0], row[1]); // This! 
    ListGaVisitors.Add(GaVisits); 
    } 

結果:

enter image description here

相關問題