我正在尝试使用SSIS中的脚本组件使用JSON。我正在尝试从此处获取数据:https://api.opendota.com/api/teams 不幸的是,在我执行程序包之后,它只给我第一行数据。
#region Class
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
/// <summary>Outputs records to the output buffer</summary>
public override void CreateNewOutputRows()
{
//Set Webservice URL
string wUrl = "https://api.opendota.com/api/teams?json";
try
{
RootObject[] Teams = GetWebServiceResult(wUrl);
//For each group of metrics output records
foreach (var metric in Teams)
{
Output0Buffer.AddRow();
Output0Buffer.teamid = metric.team_id;
Output0Buffer.rating = metric.rating;
Output0Buffer.wins = metric.wins;
Output0Buffer.losses = metric.losses;
Output0Buffer.lastmatchtime = metric.last_match_time;
Output0Buffer.name = metric.name;
Output0Buffer.tag = metric.tag;
Output0Buffer.logourl = metric.logo_url;
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
/// <summary>
/// Method to return our WorkGroupMetric array
/// </summary>
/// <param name="wUrl">The web service URL to call</param>
/// <returns>An array of WorkGroupMetric composed of the de-serialized JSON</returns>
private RootObject[] GetWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
RootObject[] jsonResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
string jsonString = null;
//Set jsonString using a stream reader
using (StreamReader reader = new StreamReader(responseStream))
{
jsonString = reader.ReadToEnd();
reader.Close();
}
//Deserialize our JSON
JavaScriptSerializer sr = new JavaScriptSerializer();
jsonResponse = sr.Deserialize<RootObject[]>(jsonString);
}
//Output connection error message
else
{
FailComponent(httpWResp.StatusCode.ToString());
}
}
//Output JSON parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return jsonResponse;
}
/// <summary>
/// Outputs error message
/// </summary>
/// <param name="errorMsg">Full error text</param>
private void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
}
#endregion
#region JSON Class
//Class to hold data
public class RootObject
{
public int team_id { get; set; }
public double rating { get; set; }
public int wins { get; set; }
public int losses { get; set; }
public int last_match_time { get; set; }
public string name { get; set; }
public string tag { get; set; }
public string logo_url { get; set; }
}
#endregion
我唯一能想到的是JSON开头以[]结尾,我真的不确定bot。
我找到的关于类似问题的唯一答案是:
实际上我确实可以使用它。问题与JSON或Class无关,而在于每次调用仅返回一条记录这一事实。我干脆摆脱了For Each循环,嘿,很容易
但不确定如何使它们互不相干。