2017-06-22 52 views
1

我有一個反序列化問題。以下是錯誤返回:反序列化錯誤 - 列表和對象

因爲類型需要JSON對象(例如{「名稱」不能反序列化當前JSON陣列(例如[1,2,3])轉換成類型「SmartTransportNatif.Stations」:」值「})來正確地反序列化。

"stations": { 
     "from": [ 
      { 
       "id": "008501120", 
       "name": "Lausanne", 
       "score": 101, 
       "coordinate": { 
        "type": "WGS84", 
        "x": 46.516777, 
        "y": 6.629095 
       }, 
       "distance": null 
      } 
     ], 
     "to": [ 
      { 
       "id": "000000178", 
       "name": "Fribourg", 
       "score": null, 
       "coordinate": { 
        "type": "WGS84", 
        "x": 46.803272, 
        "y": 7.151027 
       }, 
       "distance": null 
      } 
     ] 
    } 

這裏是我的對象進行反序列化:

public class RootObject2 
    { 
     public List<Connection> connections { get; set; } 
     public Station from { get; set; } 
     public Station to { get; set; } 
     public Stations stations { get; set; } 
    } 

public class Stations 
    { 
     [JsonProperty("from")] 
     public List<Station> from { get; set; } 

     [JsonProperty("to")] 
     public List<Station> to { get; set; } 
    } 


    public class Station 
    { 
     public string id { get; set; } 
     public string name { get; set; } 
     public object score { get; set; } 
     public Coordinate coordinate { get; set; } 
     public double distance { get; set; } 
    } 

由於錯誤中提到,它的類型是「

我有一個JSON的這部分從服務器返回站「導致的錯誤,但我不明白什麼是錯的。

任何人都可以幫助我嗎?

在此先感謝

+0

答案很簡單:這個JSON是**無效**。 JSON應該代表一個對象或一個數組。 –

+0

@YeldarKurmangaliyev這是一個長JSON的最終屬性 –

+1

好的,我明白了。您需要發佈最小有效的JSON,以便每個人都可以重現此問題。你可以把整個JSON和刪除所有不必要的項目,但它應該是有效的。 –

回答

0

讓我們的工作向後首先,這裏有你的類:

看着這些用心,因爲我有正確映射類型每個屬性,基於在你的json對象上。

public class RootObject 
{ 
    public Stations Stations { get; set; } 
} 

public class Stations 
{ 
    public List<Station> From { get; set; } 
    public List<Station> To { get; set; } 
} 


public class Station 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
    public int? Score { get; set; } 
    public Coordinate Coordinate { get; set; } 
    public double? Distance { get; set; } 
} 

public struct Coordinate 
{ 
    public string Type { get; set; } 
    public double X { get; set; } 
    public double Y { get; set; } 
} 

這裏是你的類,構造,使您的期望RootObject的樣本。

var data = new RootObject() 
{ 
    Stations = new Stations() 
    { 
     From = new List<Station>() 
     { 
      new Station() 
      { 
       Id = "008501120", 
       Name = "Lausanne", 
       Score = 101, 
       Coordinate = new Coordinate {Type = "WGS84", X = 46.516777, Y = 6.629095}, 
       Distance = null 
      } 
     }, 
     To = new List<Station>() 
     { 
      new Station() 
      { 
       Id = "000000178", 
       Name = "Fribourg", 
       Score = null, 
       Coordinate = new Coordinate {Type = "WGS84", X = 46.803272, Y = 7.151027}, 
       Distance = null 
      } 
     } 
    } 
}; 

var json = Newtonsoft.Json.JsonConvert.SerializeObject(data); 

你得到的價值是這樣的:

{ 
    "Stations": { 
    "From": [ 
     { 
     "Id": "008501120", 
     "Name": "Lausanne", 
     "Score": 101, 
     "Coordinate": { 
      "Type": "WGS84", 
      "X": 46.516777, 
      "Y": 6.629095 
     }, 
     "Distance": null 
     } 
    ], 
    "To": [ 
     { 
     "Id": "000000178", 
     "Name": "Fribourg", 
     "Score": null, 
     "Coordinate": { 
      "Type": "WGS84", 
      "X": 46.803272, 
      "Y": 7.151027 
     }, 
     "Distance": null 
     } 
    ] 
    } 
} 

這個工作在反向罰款。

var foo = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json); 

證明它的工作原理根據您的鏈接

http://transport.opendata.ch/v1/connections?from=lausanne&to=fribourg
以上,這裏都在行動

var httpWebRequest = (HttpWebRequest)WebRequest 
    .Create("http://transport.opendata.ch/v1/connections?from=lausanne&to=fribourg"); 
httpWebRequest.ContentType = "application/json"; 
httpWebRequest.Method = "GET"; 
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 

using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) 
{ 
    var result = streamReader.ReadLine(); 
    var foo = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(result); 
} 

工作正常類。


你的錯誤消息

您得到的誤差:

無法反序列化當前JSON陣列(例如[1,2,3])轉換成類型「SmartTransportNatif.Stations ',因爲該類型需要一個JSON對象(例如{「name」:「value」})來正確反序列化。

這指向一個你甚至沒有在你的問題中顯示的類,它只是被錯誤地建模爲json。但是因爲它不是與你的問題一起提供的,所以上面的答案足以顯示出錯的地方。

+0

那麼您獲得的價值與我收到的價格完全相同。我推斷這些類是正確的。我改變了類型以符合你的解決方案,但我仍然有同樣的問題。 –

+0

@ A.Silva - 我已經添加了示例代碼,以便在最近的編輯中向您證明。 – Svek

+0

你的回答是完全正確的,我發現我的問題是另一個問題,我用於反序列化的JSON變量與之前的另一個請求具有相同的值。所以JSON不是我期待的那個。 謝謝 –

0

我假設你缺少在前面,並在年底{}。如果是的話,下面的檢查:從它

public class Coordinate 
{ 
    public string type { get; set; } 
    public double x { get; set; } 
    public double y { get; set; } 
} 

public class From 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
    public int score { get; set; } 
    public Coordinate coordinate { get; set; } 
    public object distance { get; set; } 
} 

public class Coordinate2 
{ 
    public string type { get; set; } 
    public double x { get; set; } 
    public double y { get; set; } 
} 

public class To 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
    public object score { get; set; } 
    public Coordinate2 coordinate { get; set; } 
    public object distance { get; set; } 
} 

public class Stations 
{ 
    public List<From> from { get; set; } 
    public List<To> to { get; set; } 
} 

public class RootObject 
{ 
    public Stations stations { get; set; } 
} 
+0

從下到上 – LONG

+0

爲什麼會有「From」和「To」不同的對象,如果他們有相同的屬性? –