我正在尝试从BLS网站请求一组特定的数据:
http://www.bls.gov/developers/api_signature_v2.htm#parameters
在该链接上,我特意试图获得一个带有可选参数的系列。我非常接近,但好像我的JSON帖子请求没有被处理。
static void TryParse()
{
//Get JSON data
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("http://api.bls.gov/publicAPI/v2/timeseries/data/CUUR0000SA0");
//Deserialize JSON data
var p1 = new JavaScriptSerializer();
RootObject j = p1.Deserialize<RootObject>(json);
RootObject r1 = JsonConvert.DeserializeObject<RootObject>(json);
//check to see if JSON data was successfully downloaded
if (r1.ToString() == "Status: REQUEST_SUCCEEDED")
{
//Loop through the JSON to find values
foreach (Datum d in j.Results.series[0].data)
{
//Filters data based on year value
if ((Int16.Parse(d.year) == DateTime.Now.Year - 1 && d.period == "M10" )) //|| (Int16.Parse(d.year) == DateTime.Now.Year - 1 && d.period == "M10" )))
{
octLY = (double.Parse(d.value));
Console.WriteLine("OCT14: {0}", octLY);
//Console.WriteLine(d.year + " : " + d.period + " : " + d.periodName + " : " + d.value);
}
if (Int16.Parse(d.year) == DateTime.Now.Year - 2 && d.period == "M10")
{
oct2y = (double.Parse(d.value));
Console.WriteLine("OCT13: {0}", oct2y);
}
else { }
}
Console.WriteLine("CPI: {0}", (octLY - oct2y) / oct2y + 1);
}
else
{
Console.WriteLine(r1.ToString());
Console.ReadLine();
}
Console.ReadLine();
}
}
我在JSON帖子数据中硬编码以完全符合BLS链接上的示例如何显示。问题是它忽略了我的所有参数。尽管要求5等等,我只获得了3年的数据......
可以使用我的身份验证代码,因为这是通用的,稍后会更改。
有人可以告诉我如何使用POST方法请求这些数据吗?我觉得我只是缺少一些基本的东西,但却无法得到它。
仅供参考,如果您要重新创建此问题,它将很容易,因为它是所有开源数据等...但由于它未在请求中看到我的授权,因此它&#39 ; s限制用户每天25个请求...所以如果你按原样运行26次...你将收到超出请求的消息。如果这个工作从25次变为500次。
这是我尝试的另一种方法:
{{1}}
答案 0 :(得分:3)
使用您的代码重新创建我能够在RootObject类Results.Series集合中获取有效数据。首先,我创建了一个新的Class Called Series Post。
public class SeriesPost
{
public string[] seriesid { get; set; }
public string startyear { get; set; }
public string endyear { get; set; }
public bool catalog { get; set; }
public bool calculations { get; set; }
public bool annualaverage { get; set; }
public string registrationKey { get; set; }
}
从那里我稍微修改了你的JSON系列化
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string newJson = Newtonsoft.Json.JsonConvert.SerializeObject(new SeriesPost()
{
seriesid = (new List<string>() { "CUUR0000SA0" }).ToArray(),
startyear = "2010",
endyear = "2015",
catalog = false,
calculations = true,
annualaverage = true,
registrationKey = "f3171173a8ce4b969b5085ba9a83202f"
});
//So you can see the JSON thats output
System.Diagnostics.Debug.WriteLine(newJson);
streamWriter.Write(newJson);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
//Here your RootObject is the Type thats returned with your results
RootObject obj = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(result);
// Loop over the series in the results
foreach(Series ser in obj.Results.series)
{
// Loop over the Data in each of the series.
foreach(var data in ser.data)
{
//Output the year, in my test I got multiple entries for each year between 2010 to 2015
Console.WriteLine(data.year);
}
}
Console.ReadLine();
}