如何从rest Web服务响应中获取特定部分的数据
本文关键字:获取 定部 数据 响应 rest Web 服务 | 更新日期: 2023-09-27 18:29:13
我是rest Web服务的新手,我试图了解如何使用它们。
我在C#中定义了一个http获取请求,如下所示:
static string HttpGet(string url)
{
HttpWebRequest req = WebRequest.Create(url)
as HttpWebRequest;
string result = null;
using (HttpWebResponse resp = req.GetResponse()
as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
return result;
}
然后我在一个按钮中使用HttpGet从THIS weather webservice获取xml数据(我放入txtOut以发现我的代码有效)。
private void btnGet_Click(object sender, RoutedEventArgs e)
{
string test = HttpGet("http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml");
txtOut.Text = test;
}
它从发送get请求到该url获取整个xml:http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml
所以我的问题是如何将xml的特定部分保存在变量中?比如卡尔文的最低温度,所以我可以把它转换成华氏度或摄氏度。
请帮帮我。
你的api也返回json,所以你可以像下面这样使用它(使用json.Net)
using (WebClient wc = new Webclient())
{
var json = wc.DownloadString("http://api.openweathermap.org/data/2.5/weather?q=London&mode=json");
var obj = JsonConvert.DeserializeObject<OpenWeatherMap.Root>(json);
}
public class OpenWeatherMap
{
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public class Sys
{
public int type { get; set; }
public int id { get; set; }
public double message { get; set; }
public string country { get; set; }
public int sunrise { get; set; }
public int sunset { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Main
{
public double temp { get; set; }
public int humidity { get; set; }
public double pressure { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
}
public class Wind
{
public double speed { get; set; }
public double gust { get; set; }
public int deg { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Root
{
public Coord coord { get; set; }
public Sys sys { get; set; }
public List<Weather> weather { get; set; }
public string @base { get; set; }
public Main main { get; set; }
public Wind wind { get; set; }
public Dictionary<string,double> rain { get; set; }
public Clouds clouds { get; set; }
public int dt { get; set; }
public int id { get; set; }
public string name { get; set; }
public int cod { get; set; }
}
}
如果你坚持使用xml,那么你可以使用Linq2Xml+XPath
var xDoc = XDocument.Load("http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml");
var windSpeed = (double)xDoc.XPathSelectElement("//wind/speed").Attribute("value");
或
var temp = (double)xDoc.Root.Element("temperature").Attribute("value");
您可以使用Newtonsoft.json json-converot或任何其他可以将json转换为动态的转换器来获得json响应并转换为动态obj。如果返回对象的方案发生更改,这将帮助您避免解析错误。
using (WebClient wc = new Webclient())
{
var json = wc.DownloadString("http://api.openweathermap.org/data/2.5/weather?q=London&mode=json");
dynamic jsonResult = JsonConvert.DeserializeObject<ExpandoObject>(json , new ExpandoObjectConverter());
// using dynamic object
var lon = jsonResult.coord.lon;
}