2016-07-28 3 views
0
Controller 
{ 
namespace WebApplication10.Controllers 
{ 
    public class Default1Controller : ApiController 
    { 
     [ResponseType(typeof(WeatherItem))] 
     public WeatherItem Post(Wcall call) 
     { 

      string apikey = //MYAPIKEY; 
      string url = string.Format("http://api.openweathermap.org/data/2.5/forecast/daily?q={0}&units=metric&cnt=1&APPID={1}", call.city1, apikey); 
      WebClient A1 = new WebClient(); 
      string dataweather = A1.DownloadString(url); 

      WeatherItem a2 = JsonConvert.DeserializeObject<WeatherItem>(dataweather); 
      string przyklad = a2.list.pressure; 
      return a2; 
     } 
    } 
} 
} 
class 
{ 
namespace WebApplication10.Models 
{ 
    public class WeatherItem 
    { 
     public List list { get; set; } 
     public string cod { get; set; } 
     public string message { get; set; } 
     public int cnt { get; set; } 
     //public List<City> city { get; set; }  
    } 
    public class List 
    { 
     public int dt { get; set; } 
     // public List<Temp> temp { get; set; } 
     public string pressure { get; set; } 
     public int humidity { get; set; } 
     // public List<Weather> weather { get; set; } 
     public string speed { get; set; } 
     public string deg { get; set; } 
     public string clauds { get; set; } 
     public int rain { get; set; } 
    } 
    public class Wcall 
    { 
     public string city1 { get; set; } 
     public string street { get; set; } 
    } 
    public class City 
    { 
     public int id { get; set; } 
     public string name { get; set; } 
     //public List<Coords> coords { get; set; } 
     public string country { get; set; } 
     public int population { get; set; } 
    } 
    public class Coords 
    { 
     public string lon { get; set; } 
     public string lat { get; set; } 
    } 
    public class Temp 
    { 
     public string day { get; set; } 
     public string min { get; set; } 
     public string max { get; set; } 
     public string night { get; set; } 
     public string eve { get; set; } 
     public string morn { 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; } 
    } 
} 
} 

예외 : 'Newtonsoft.Json.JsonSerializationException가' Newtonsoft.Json.dll을 발생하지만, 사용자 코드 추가 정보 처리되지 않은 유형의 예외 : 전류를 역 직렬화 할 수 없습니다 JSON 개체 (예 : { "이름": "값"}) ... 내 영어 죄송합니다 JSON 직렬화 오류 C#을 MVC

가, 누군가가 내가 잘못하고 무슨 생각을?

+2

'dataweather'로 돌아갈 값을 추가하십시오. –

답변

1

Visual Studio에는 json을 C# 모델로 변환하는 내장 기능이 있습니다.

당신은 당신의 질문에 대해 Edit\Paste Special\Parse Json as classes

에서 찾을 수 있습니다, 모델이 유효하지 않습니다,이 시도.

public class WeatherItem 
{ 
    public City city { get; set; } 
    public string cod { get; set; } 
    public float message { get; set; } 
    public int cnt { get; set; } 
    public List[] list { get; set; } 
} 

public class City 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public Coord coord { get; set; } 
    public string country { get; set; } 
    public int population { get; set; } 
} 

public class Coord 
{ 
    public float lon { get; set; } 
    public float lat { get; set; } 
} 

public class List 
{ 
    public int dt { get; set; } 
    public Temp temp { get; set; } 
    public float pressure { get; set; } 
    public int humidity { get; set; } 
    public Weather[] weather { get; set; } 
    public float speed { get; set; } 
    public int deg { get; set; } 
    public int clouds { get; set; } 
    public float rain { get; set; } 
} 

public class Temp 
{ 
    public float day { get; set; } 
    public float min { get; set; } 
    public float max { get; set; } 
    public float night { get; set; } 
    public float eve { get; set; } 
    public float morn { 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; } 
}