我试图用json中的数据填充我的班级。但是我在获取今天和预测的代码列表时遇到问题?
string js = @"{"query":{"count":1,"created":"2013-04-28T11:10:11Z","lang":"en-US","results":{"channel":{"item":{"title":"Conditions for Kazan', RS at 3:01 pm MSK","lat":"55.78","long":"49.18","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Kazan'__RS/*http://weather.yahoo.com/forecast/RSXX0043_f.html","pubDate":"Sun, 28 Apr 2013 3:01 pm MSK","condition":{"code":"30","date":"Sun, 28 Apr 2013 3:01 pm MSK","temp":"54","text":"Partly Cloudy"},"description":"\n<img src=\"http://l.yimg.com/a/i/us/we/52/30.gif\"/><br />\n<b>Current Conditions:</b><br />\nPartly Cloudy, 54 F<BR />\n<BR /><b>Forecast:</b><BR />\nSun - Light Rain Late. High: 57 Low: 44<br />\nMon - Rain. High: 59 Low: 36<br />\n<br />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Kazan'__RS/*http://weather.yahoo.com/forecast/RSXX0043_f.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n","forecast":[{"code":"11","date":"28 Apr 2013","day":"Sun","high":"57","low":"44","text":"Light Rain Late"},{"code":"12","date":"29 Apr 2013","day":"Mon","high":"59","low":"36","text":"Rain"}],"guid":{"isPermaLink":"false","content":"RSXX0043_2013_04_29_7_00_MSK"}}}}}}";
答案 0 :(得分:3)
使用此代替上面的代码
public class Condition
{
public string code { get; set; }
public string date { get; set; }
public string temp { get; set; }
public string text { get; set; }
}
public class Forecast
{
public string code { get; set; }
public string date { get; set; }
public string day { get; set; }
public string high { get; set; }
public string low { get; set; }
public string text { get; set; }
}
public class Guid
{
public string isPermaLink { get; set; }
public string content { get; set; }
}
public class Item
{
public string title { get; set; }
public string lat { get; set; }
public string @long { get; set; }
public string link { get; set; }
public string pubDate { get; set; }
public Condition condition { get; set; }
public string description { get; set; }
public List<Forecast> forecast { get; set; }
public Guid guid { get; set; }
}
public class Channel
{
public Item item { get; set; }
}
public class Results
{
public Channel channel { get; set; }
}
public class Query
{
public int count { get; set; }
public string created { get; set; }
public string lang { get; set; }
public Results results { get; set; }
}
public class RootObject
{
public Query query { get; set; }
}
使用httpclient发送请求
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(uri);
string content = await response.Content.ReadAsStringAsync();
RootObject obj = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(content);
答案 1 :(得分:2)
Parse方法需要JSON
,而是给它一个URL而不是......
下载网址
的内容var json = (new WebClient()).DownloadString(url);
然后
JObject o = JObject.Parse(json);
UPD :发帖后彻底改变问题不是一个好习惯。
最初您尝试将URL
解析为JSON
,此答案显示了
UPD2 :也许您现在发布的JSON无效, 例如JsonLint says
Parse error on line 9:
... "title": "ConditionsforKazan'
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
UPD3 好的,现在看起来有效。
上查看这些样本答案 2 :(得分:1)
传递一个JSON字符串,然后尝试解析它。 NewtonSoft只接受Json字符串。你通过的是一个查询字符串。