2013-06-11 131 views
1

獲取和填充JSON數據時,我得到一個奇怪的錯誤:Newtonsoft.Json.JsonSerializationException的Windows Phone 8

型「Newtonsoft.Json.JsonSerializationException」 的異常出現在Newtonsoft.Json.DLL但 成型「WeatherApp.WeatherData + RootObject []」因爲類型 需要JSON陣列(例如:在用戶代碼

無法反序列化當前JSON對象(例如{「值」,「名稱」})沒有處理[1,2,3])來正確地反序列化。要解決此 錯誤或者改變JSON到JSON陣列(例如[1,2,3])或 變化反序列化類型,以便它是一個正常的.NET類型(例如 不是原始類型像整數,而不是一個集合類型,如數組 或List),可以從JSON對象反序列化。 JsonObjectAttribute也可加入到將其從JSON對象強制 反序列化類型。路徑 '座標',1號線,9號位置

這裏是我JSON類:

Public Class WeatherData 
    Public Class Coord 
     Public Property lon() As Double 
      Get 
       Return m_lon 
      End Get 
      Set(value As Double) 
       m_lon = Value 
      End Set 
     End Property 
     Private m_lon As Double 
     Public Property lat() As Double 
      Get 
       Return m_lat 
      End Get 
      Set(value As Double) 
       m_lat = Value 
      End Set 
     End Property 
     Private m_lat As Double 
    End Class 

    Public Class Sys 
     Public Property country() As String 
      Get 
       Return m_country 
      End Get 
      Set(value As String) 
       m_country = Value 
      End Set 
     End Property 
     Private m_country As String 
     Public Property sunrise() As Integer 
      Get 
       Return m_sunrise 
      End Get 
      Set(value As Integer) 
       m_sunrise = Value 
      End Set 
     End Property 
     Private m_sunrise As Integer 
     Public Property sunset() As Integer 
      Get 
       Return m_sunset 
      End Get 
      Set(value As Integer) 
       m_sunset = Value 
      End Set 
     End Property 
     Private m_sunset As Integer 
    End Class 

    Public Class Weather 
     Public Property id() As Integer 
      Get 
       Return m_id 
      End Get 
      Set(value As Integer) 
       m_id = Value 
      End Set 
     End Property 
     Private m_id As Integer 
     Public Property main() As String 
      Get 
       Return m_main 
      End Get 
      Set(value As String) 
       m_main = Value 
      End Set 
     End Property 
     Private m_main As String 
     Public Property description() As String 
      Get 
       Return m_description 
      End Get 
      Set(value As String) 
       m_description = Value 
      End Set 
     End Property 
     Private m_description As String 
     Public Property icon() As String 
      Get 
       Return m_icon 
      End Get 
      Set(value As String) 
       m_icon = Value 
      End Set 
     End Property 
     Private m_icon As String 
    End Class 

    Public Class Main 
     Public Property temp() As Double 
      Get 
       Return m_temp 
      End Get 
      Set(value As Double) 
       m_temp = Value 
      End Set 
     End Property 
     Private m_temp As Double 
     Public Property pressure() As Integer 
      Get 
       Return m_pressure 
      End Get 
      Set(value As Integer) 
       m_pressure = Value 
      End Set 
     End Property 
     Private m_pressure As Integer 
     Public Property temp_min() As Double 
      Get 
       Return m_temp_min 
      End Get 
      Set(value As Double) 
       m_temp_min = Value 
      End Set 
     End Property 
     Private m_temp_min As Double 
     Public Property temp_max() As Double 
      Get 
       Return m_temp_max 
      End Get 
      Set(value As Double) 
       m_temp_max = Value 
      End Set 
     End Property 
     Private m_temp_max As Double 
     Public Property humidity() As Integer 
      Get 
       Return m_humidity 
      End Get 
      Set(value As Integer) 
       m_humidity = Value 
      End Set 
     End Property 
     Private m_humidity As Integer 
    End Class 

    Public Class Wind 
     Public Property speed() As Double 
      Get 
       Return m_speed 
      End Get 
      Set(value As Double) 
       m_speed = Value 
      End Set 
     End Property 
     Private m_speed As Double 
     Public Property deg() As Double 
      Get 
       Return m_deg 
      End Get 
      Set(value As Double) 
       m_deg = Value 
      End Set 
     End Property 
     Private m_deg As Double 
    End Class 

    Public Class Rain 
     Public Property __invalid_name__1h() As Double 
      Get 
       Return m___invalid_name__1h 
      End Get 
      Set(value As Double) 
       m___invalid_name__1h = Value 
      End Set 
     End Property 
     Private m___invalid_name__1h As Double 
    End Class 

    Public Class Clouds 
     Public Property all() As Integer 
      Get 
       Return m_all 
      End Get 
      Set(value As Integer) 
       m_all = Value 
      End Set 
     End Property 
     Private m_all As Integer 
    End Class 

    Public Class RootObject 
     Public Property coord() As Coord 
      Get 
       Return m_coord 
      End Get 
      Set(value As Coord) 
       m_coord = Value 
      End Set 
     End Property 
     Private m_coord As Coord 
     Public Property sys() As Sys 
      Get 
       Return m_sys 
      End Get 
      Set(value As Sys) 
       m_sys = Value 
      End Set 
     End Property 
     Private m_sys As Sys 
     Public Property weather() As List(Of Weather) 
      Get 
       Return m_weather 
      End Get 
      Set(value As List(Of Weather)) 
       m_weather = Value 
      End Set 
     End Property 
     Private m_weather As List(Of Weather) 
     Public Property base() As String 
      Get 
       Return m_base 
      End Get 
      Set(value As String) 
       m_base = Value 
      End Set 
     End Property 
     Private m_base As String 
     Public Property main() As Main 
      Get 
       Return m_main 
      End Get 
      Set(value As Main) 
       m_main = Value 
      End Set 
     End Property 
     Private m_main As Main 
     Public Property wind() As Wind 
      Get 
       Return m_wind 
      End Get 
      Set(value As Wind) 
       m_wind = Value 
      End Set 
     End Property 
     Private m_wind As Wind 
     Public Property rain() As Rain 
      Get 
       Return m_rain 
      End Get 
      Set(value As Rain) 
       m_rain = Value 
      End Set 
     End Property 
     Private m_rain As Rain 
     Public Property clouds() As Clouds 
      Get 
       Return m_clouds 
      End Get 
      Set(value As Clouds) 
       m_clouds = Value 
      End Set 
     End Property 
     Private m_clouds As Clouds 
     Public Property dt() As Integer 
      Get 
       Return m_dt 
      End Get 
      Set(value As Integer) 
       m_dt = Value 
      End Set 
     End Property 
     Private m_dt As Integer 
     Public Property id() As Integer 
      Get 
       Return m_id 
      End Get 
      Set(value As Integer) 
       m_id = Value 
      End Set 
     End Property 
     Private m_id As Integer 
     Public Property name() As String 
      Get 
       Return m_name 
      End Get 
      Set(value As String) 
       m_name = Value 
      End Set 
     End Property 
     Private m_name As String 
     Public Property cod() As Integer 
      Get 
       Return m_cod 
      End Get 
      Set(value As Integer) 
       m_cod = Value 
      End Set 
     End Property 
     Private m_cod As Integer 
    End Class 
End Class 

我打電話JsonConvert.DeserializeObject(Of RootObject())(content)其中content是一個字符串持有0​​

任何幫助將是非常非常感激。

+0

你能爲我們提供你試圖反序列化JSON字符串? –

+0

http://api.openweathermap.org/data/2.5/weather?q=London,uk這裏,@MartinSuchan – noobprohacker

回答

1

變化

JsonConvert.DeserializeObject(Of RootObject())(content) 

JsonConvert.DeserializeObject(Of RootObject)(content) 

你反序列化對象,而不是對象的數組。

+0

謝謝!我如何使用存儲在JSON類中的數據? – noobprohacker

+0

'JsonConvert.DeserializeObject'方法返回一個值(在你的情況'RootObject'),你只需要將其分配給一個變量。順便說一下,代碼中有很多冗餘。您可能想使用[自動執行的屬性](http://msdn.microsoft.com/en-us/library/dd293589.aspx)。 – Athari

+0

@noobprohacker如果你找到答案有用的,請不要忘記將其標記爲接受和/或給予好評的。 – Athari