我想请求一组特定的从BLS网站的数据:如何使用'POST'方法使用c#获取JSON数据?
http://www.bls.gov/developers/api_signature_v2.htm#parameters
该链接,我特别想获得一个系列可选参数。我超级接近,但它就好像我的JSON发布请求没有被处理。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Net;
using System.Data.SqlClient;
using System.Web;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.ComponentModel;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
namespace ConsoleApplication5
{
//Classes needed to deserialze JSON data (JSON to C# website)
public class DataObject
{
public string Name { get; set; }
}
public class Footnote
{
}
public class Datum
{
public string year { get; set; }
public string period { get; set; }
public string periodName { get; set; }
public string value { get; set; }
public List<Footnote> footnotes { get; set; }
}
public class Series
{
public string seriesID { get; set; }
public List<Datum> data { get; set; }
}
public class Results
{
public List<Series> series { get; set; }
}
public class RootObject
{
public string status { get; set; }
public int responseTime { get; set; }
public List<object> message { get; set; }
public Results Results { get; set; }
public override string ToString()
{
return string.Format("Status: {0}", status);
}
}
class Program
{
static double octLY;
static double oct2y;
static void Main(string[] args)
{
TryParsing();
}
static void TryParsing()
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.bls.gov/publicAPI/v2/timeseries/data/CUUR0000SA0");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
//string json = new JavaScriptSerializer().Serialize(new
//{
// seriesid = "CUUR0000SA0",
// startYear = "2010",
// endYear = "2015",
// catalog = "true",
// calculations = "true",
// annualAverage = "true",
// registrationKey = "f3171173a8ce4b969b5085ba9a83202f"
//});
string json = "{\"seriesid\":[\"CUUR0000SA0\"],\"startyear\":\"2010\",\"endyear\":\"2015\",\"catalog\":true,\"calculations\":true,\"annualAverage\":true,\"registrationKey\":\"f3171173a8ce4b969b5085ba9a83202f\"}";
//Console.WriteLine(json.ToString());
//Console.ReadLine();
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result.ToString());
Console.ReadLine();
}
}
}
}
我在JSON发布数据中进行了硬编码,以便完全符合BLS链接上的示例。问题是它忽略了我所有的参数。虽然要求5等,但我只获得3年的数据...
使用我的授权码是很好的,因为这是通用的,稍后会更改。
有人可以告诉我如何使用POST方法请求这些数据吗?我觉得我只是想念一些基本的东西,但无法得到它。如果你重新创建这个问题,它会很容易,因为它是所有的开源数据等等......但由于它没有在请求中看到我的授权,它每天限制用户25个请求。 ..所以如果你运行这个26倍......你会得到一个超出请求的消息。如果这得到工作,从25倍变为500
这里是我的其他的方法,我是想:
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();
}
}
我不认为你的seriesid应该被追加到您正试图张贴到自包含的JSON有效载荷中的URL。这是我看到的一个问题,当我重新创建您的代码时,我根据您提供的信息返回0结果消息。 – Bearcat9425
我已经删除它,但仍然只能得到最后3年......它似乎没有任何影响我的结束,据我所得到的网站....我得到JSON数据,但它不是标准我发布。 – user3486773
我想也是在你的json年度平均值应该是年度通知第一个上限A的。当我纠正我收到的数据。 – Bearcat9425