[英]Newtonsoft Parse Json Error
I'm trying to fill my class with data from json. 我试图用来自json的数据填充我的班级。 But I have a problem with getting list of codes for today and from forecast? 但是我在获取今天和预测的代码清单时遇到问题吗?
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"}}}}}}";
use this instead of above code 用它代替上面的代码
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; }
}
using httpclient send the request 使用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);
Parse method expects JSON
, you give it an URL instead... 解析方法需要JSON
,您改为给它提供一个URL ...
Download contents of URL 下载URL的内容
var json = (new WebClient()).DownloadString(url);
and then 接着
JObject o = JObject.Parse(json);
UPD : it's not a good practice to completely change question after posting. UPD :在发布后完全更改问题不是一个好习惯。 Originally you tried to parse URL
as JSON
, this answer shows how 最初,您尝试将URL
解析为JSON
,此答案显示了如何
UPD2 : maybe JSON you posted now it's not valid, eg JsonLint says UPD2 :也许您现在发布的JSON无效,例如JsonLint说
Parse error on line 9:
... "title": "ConditionsforKazan'
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
UPD3 OK, it looks valid now. UPD3好,它现在看起来有效。
Check out these samples on querying using JSON.NET 在使用JSON.NET进行查询时查看这些样本
pass a JSON string and then try to parse it. 传递JSON字符串,然后尝试解析它。 Only Json strings are acceptable by NewtonSoft. NewtonSoft仅接受Json字符串。 what ur passing is a querystring. 您传递的是一个查询字符串。
Use JSON2CSHARP 使用JSON2CSHARP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.