2013-04-28 2 views
0

json의 데이터로 수업을 채우려고합니다. 하지만 오늘과 예측에서 코드 목록을 가져 오는 데 문제가 있습니까? 사용이 대신 위의 코드Newtonsoft Parse Json Error

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

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

나는 그것이 당신을위한 URL ... –

답변

3

보내는 방법은 JSON 기대

var httpClient = new HttpClient(); 
var response = await httpClient.GetAsync(uri); 


string content = await response.Content.ReadAsStringAsync(); 


RootObject obj = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(content); 
+0

좋은 일, 정말 좋은 데이터를 방문 생각하지 않는다! –

+0

코드를 생성하기 위해 [json2csharp] (http://json2csharp.com/)를 사용하고 추천합니다 –

2

Parse, 당신이 그것을 대신 URL 제공 요청을 HttpClient를를 사용하여 ...

URL 내용 다운로드

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

다음

JObject o = JObject.Parse(json); 

UPD : 완전히 게시 후 질문을 변경하는 것이 좋습니다 아니다. 원래는 JSONURL을 구문 분석을 시도,이 답변이 방법

UPD2을 보여줍니다 당신이 유효하지의 지금 게시 어쩌면 JSON 예를 들어 JsonLint says

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

UPD3는 OK, 지금 유효한 보이는 .

체크 아웃

+0

죄송합니다. 실수였습니다. 서둘러야 할 경우가 있습니다. –

+0

고맙습니다 만, 어떻게 " 코드에서 그것? –

+0

선택 : Json이 유효합니다! –

1

querying using JSON.NET에 이러한 샘플은 JSON 문자열을 통과 한 후 구문 분석하려고합니다. Json 문자열 만 NewtonSoft에서 허용됩니다. 통과하는 것은 querystring입니다.

사용 JSON2CSHARP