2017-06-22 8 views
1

逆シリアル化に問題があります。ここでエラーが返されます。タイプはJSONオブジェクトを必要とするため逆シリアル化でエラーが発生しました - リストとオブジェクト

は型「SmartTransportNatif.Stations」内に現在のJSON配列(例えば[1,2,3])をデシリアライズすることはできません(例:{「名」:」 value "})を使用して正しくデシリアライズします。

私は、JSONのこの部分は、サーバから返されています:

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

エラーが述べているように、それは "タイプですエラーの原因となる「ステーション」が表示されますが、何が間違っているかわかりません。

誰でも手伝ってもらえますか?

おかげで、事前

+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 
     } 
    ] 
    } 
} 

これは逆で正常に動作します。それは

http://transport.opendata.ch/v1/connections?from=lausanne&to=fribourg
あなたのリンクに基づいて動作することを

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

証明は上記、ここでアクション

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.シルバ - 私は最近の編集であなたにそれを証明するためのサンプルコードを追加しました。 – 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」があるのですか?彼らは同じ属性を持っている場合は別個のオブジェクト? –

関連する問題