0

我正在尝试用来自 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"}}}}}}";
4

3 回答 3

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);
于 2013-04-28T12:21:32.097 回答
2

Parse方法期望JSON,你给它一个 URL 代替......

下载网址内容

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

接着

JObject o = JObject.Parse(json);

UPD:发布后完全更改问题不是一个好习惯。最初你试图解析URLJSON,这个答案显示了如何

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

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

UPD3好的,现在看起来有效。

查看这些关于使用 JSON.NET 进行查询的示例

于 2013-04-28T12:22:58.217 回答
1

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

使用JSON2CSHARP

于 2013-04-28T12:24:11.920 回答