Newtonsoft解析Json错误

本文关键字:错误 Json 解析 Newtonsoft | 更新日期: 2023-09-27 18:11:08

我正试图用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"}}}}}}";

Newtonsoft解析Json错误

用这个代替上面的代码

 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);

解析方法期望JSON,你给它一个URL代替…

下载网址

var json = (new WebClient()).DownloadString(url);

然后

JObject o = JObject.Parse(json);

UPD:在发布后完全更改问题不是一个好的做法。最初您试图将URL解析为JSON,此答案显示

如何

UPD2:也许你现在发布的JSON是无效的,例如JsonLint说

Parse error on line 9:
...           "title": "ConditionsforKazan'
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

UPD3 OK,现在看起来有效了。

查看这些使用JSON查询的示例。净

传递一个JSON字符串,然后尝试解析它。NewtonSoft只接受Json字符串。你传递的是一个querystring。

使用JSON2CSHARP