2014-10-29 2 views
0

http://api.openweathermap.org/data/2.5/weather?q=Ankara,tr json.net하지만 뭔가 잘못된 추측으로 JSON을 구문 분석, 처음에는 내가 구문 분석하려고 지금이이 현재 JSON 데이터입니다

using System; 
using System.Collections.Generic; 

namespace weatherSample 
{ 
    public class service 
    { 
     public service() 
     { 
     } 
    } 

    public class Coord 
    { 
     public double lon { get; set; } 

     public double lat { get; set; } 
    } 

    public class Sys 
    { 
     public int type { get; set; } 

     public int id { get; set; } 

     public double message { get; set; } 

     public string country { get; set; } 

     public int sunrise { get; set; } 

     public int sunset { get; set; } 
    } 

    public class Weather 
    { 
     public int id { get; set; } 

     public string main { get; set; } 

     public string description { get; set; } 

     public string icon { get; set; } 
    } 

    public class MainWeather 
    { 
     public double temp { get; set; } 

     public int pressure { get; set; } 

     public int humidity { get; set; } 

     public double temp_min { get; set; } 

     public double temp_max { get; set; } 
    } 

    public class Wind 
    { 
     public double speed { get; set; } 

     public int deg { get; set; } 
    } 

    public class Clouds 
    { 
     public int all { get; set; } 
    } 

    public class RootObject 
    { 
     public Coord coord { get; set; } 

     public Sys sys { get; set; } 

     public List<Weather> weather { get; set; } 

     public string @base { get; set; } 

     public MainWeather main { get; set; } 

     public Wind wind { get; set; } 

     public Clouds clouds { get; set; } 

     public int dt { get; set; } 

     public int id { get; set; } 

     public string name { get; set; } 

     public int cod { get; set; } 
    } 
} 

를 얻을 수 JSON이 C#을 함께 만들하지만 난 몇 가지 문제가 얻을,

WebClient webC = new WebClient (link); 

var jsonDatas = JObject.Parse (y); 

var c = JsonConvert.DeserializeObject <MainWeather> (y); 

Console.Write (c.temp); 

것은 무엇을 잘못한다 0 값 반환?

RootObject result = JsonConvert.DeserializeObject<RootObject>(y); 

는 그 다음 MainWeather 속성에 액세스 :

답변

1

는 대신 RootObject의 인스턴스로 직렬화 할 필요가

Console.Write(result.main.temp); 

예 :https://dotnetfiddle.net/LWfHrH

그냥 신경 경우 MainWeather 개체 일 수도 있습니다.

MainWeather r = JObject.Parse(y)["main"].ToObject<MainWeather>(); 
+0

네, suprisely 나는 그것을 찾았습니다, 정보 btw 주셔서 감사합니다 :), 모든 json datas를위한 것입니까? 상단에서 사용해야합니까? –

+0

@ HâlukYilmaz : 당신은 항상 그와 같은'RootObject'를 deserialize해야만한다는 것을 의미합니까? –

+0

네, 안녕하세요 앤드류, 나쁜 발음에 대해 유감스럽게 생각합니다. 나는 정확하게 당신이 의미하는 바를 말하려고합니다. 아이에게서 사용하기 전에 항상 널 또는 0이됩니다. –