2016-05-04 5 views
3

にdeserialisingない、ここJSONしかし、私の属性クラスに私の属性フィールドをdeserialiseように見えることはできません、私は私のハッシュをdeserialiseすることができる午前クラス

{ 
     "attribute": { 
      "status": "FWD", 
      "type": "P2p", 
      "cost": "4", 
      "role": "Desg", 
      "priorityNo": "128.1" 
     }, 
     "hash": "50f74cc4c03637b753e884c4dfbd4270089658e5" 
    } 

、ここでは、私のC#のクラスで私のJSONオブジェクトである

public class InterfaceObject 
{ 
    public string hash { get; set; } 
    Attribute attribute { get; set; } 
} 

public class Attribute 
{ 
    public string hash { get; set; } 
    public string role { get; set; } 
    public string status { get; set; } 
    public string cost { get; set; } 
    public string priorityNo { get; set; } 
    public string type { get; set; } 
} 

私は、インタフェースオブジェクトにハッシュをdeserialiseすることができていますが、私の属性クラスをdeserialiseように見えることはできませんし、ここで私は

InterfaceObject ifa = JsonConvert.DeserializeObject<InterfaceObject>(message); 
私のオブジェクトをdeserialiseしようとする方法です。
+0

シリアライズ/デシリアライズには何を使用していますか? – DVK

+0

@DVK newtonsoft JsonConvert.DeserializeObjectを使用しています – Johnathon64

+4

デシリアライゼーションコードを示してください。 –

答えて

0

デシリアライゼーションが正しく機能しない理由は、Crowcoderが指摘したように、attributeGi1/0/1のプロパティです。このためには、既に作成されているクラスを含む包括的クラスを作成する必要があります。

このような何かを行うだろう

しかし
public class RootObject 
{ 
    public InterfaceObject iObject { get; set; } 
} 

public class InterfaceObject 
{ 
    public string hash { get; set; } 
    public Attribute attribute { get; set; } 
} 

public class Attribute 
{ 
    public string hash { get; set; } 
    public string role { get; set; } 
    public string status { get; set; } 
    public string cost { get; set; } 
    public string priorityNo { get; set; } 
    public string type { get; set; } 
}  

今、あなたのオリジナルのポストを変更したことを、あなたはJSONは、編集されたバージョンのように見えると仮定し、それをあなたが持っている道を解析することができるはずです。

4

attributeプロパティにpublicがありません。プロパティに追加の属性を追加せずに、Json.Netはprivateプロパティに値を設定できません。

public class InterfaceObject 
{ 
    public string hash { get; set; } 
    public Attribute attribute { get; set; } 
} 

publicを追加すると動作します。

+2

FYI、Json.NET *は非公開のプロパティを非直列化できますが、この動作を有効にするにはプロパティで '[JsonProperty]'属性を使用する必要があります。 –

関連する問題