2012-12-22 79 views
1

我正在開發一個移動空中應用程序。對於通信我使用的是wcf服務。 現在我的問題是,我試圖反序列化使用json.net的數組數組。子陣列是鍵入的。 所以,我的在移動端類如下:c#wcf服務反序列化json數組使用json.net

package Model.VO.PHCDATA 
{ 
    [Bindable] 
    [RemoteClass(alias="Model.PCHModel.VODadosPHC")] 
    public class VoBi 
    { 
     private var _BIstamp:String; 
     private var _ivaincl:Number; 
     private var _vendnm:String; 
     private var _vendedor:Number; 
     private var _iva:Number; 
     private var _lordem:Number; 
     private var _tabiva:Number; 
     private var _txiva:Number; 
     private var _familia:String;    
     .... 

    } 
} 

並且另一個是:

package Model.VO.PHCDATA 
{ 
    [Bindable] 
    [RemoteClass(alias="Model.PCHModel.VODadosPHC")] 

    public class VOBO 
    { 
     private var _BOstamp:String; 
     private var _vendedor:Number; 
     private var _vendnm:String; 

     .... 
    } 
} 

和相應的C#類如下:

[DataContract(Name = "VoBi")] 
    public class VoBi 
    { 
     [DataMember(IsRequired = true, Name = "BIstamp", Order = 0)] 
     public string BIstamp { get; set; } 
     [DataMember(IsRequired = true, Name = "ivaincl", Order = 1)] 
     public decimal ivaincl { get; set; } 
     [DataMember(IsRequired = true, Name = "vendnm", Order = 2)] 
     public string vendnm { get; set; } 
     [DataMember(IsRequired = true, Name = "vendedor", Order = 3)] 
     public decimal vendedor { get; set; } 
        .... 

    } 

和其他:

[DataContract(Name = "VOBO")] 
    public class VOBO 
    { 
     [DataMember(IsRequired = true, Name = "BOstamp", Order = 0)] 
     public string BOstamp { get; set; } 
     [DataMember(IsRequired = true, Name = "vendedor", Order = 1)] 
     public decimal vendedor { get; set; } 
     [DataMember(IsRequired = true, Name = "vendnm", Order = 2)] 
     public string vendnm { get; set; } 
     [DataMember(IsRequired = true, Name = "nmdos", Order = 3)] 
     public string nmdos { get; set; } 
     [DataMember(IsRequired = true, Name = "ndos", Order = 4)] 
     public decimal ndos { get; set; } 

     ..... 

    } 

被髮送到服務器的相應JSON字符串是這個:

{ 
    "DADOSBI": [ 
     { 
      "edebito": 0, 
      "desconto": 92.121, 
      "vendedor": 0, 
      "desc2": 222.343, 
      "iva": 23, 
      "ettdeb": 123.555, 
      "lordem": 12, 
      "tabiva": 2.1, 
      .... 
     }, 
     { 
      "edebito": 0, 
      "desconto": 92.121, 
      "vendedor": 1, 
      "desc2": 222.343, 
      "iva": 23, 
      "ettdeb": 123.555, 
      "lordem": 12, 
      "tabiva": 2.1, 
      .... 
     } 
    ], 
    "DADOSBO": [ 
     { 
      "estab": 123.88, 
      "etotaldeb": 123, 
      "obs": "", 
      "vendedor": 0, 
      "statuspda": "qqqqqqqqqqqq", 
      "ebo_2tvall": 12, 
      "ebo_2tdes1": 12.11, 
      ... 
     }, 
     { 
      "estab": 123.88, 
      "etotaldeb": 123, 
      "obs": "", 
      "vendedor": 1, 
      "statuspda": "qqqqqqqqqqqq", 
      "ebo_2tvall": 12, 
      .... 
     } 
    ] 
} 

我該如何處理呢?我是否使用json.net CustomCreationConverter來反序列化數據?並獲得填充了我提到的類的相應數組,或者我使用另一種方法? 幫助將不勝感激。 在此先感謝。

+0

這將是更好的,如果你可以提供更多的信息,如你如何序列化它?您是否嘗試過'System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(yourStream)',因爲在示例中沒有看到任何類型的數組(......子數組是鍵入的......),您是否可以用較少的屬性示例。 – keyr

回答

0

假設你的頂級如下所示(如果你沒有,然後創建一個,如果它是更省力,否則我們需要去一個更好的解決方案),然後

internal class TopClass 
{ 

    [JsonProperty("DADOSBI")] 
    public IList<VoBi> VOBiList { get; set; } 

    [JsonProperty("DADOSBO")] 
    public IList<VOBO> VOBOList { get; set; } 
} 

簡單deserialise使用

var topClass = JsonConvert.DeserializeObject<TopClass>(json); 

如果這不是你正在尋找的答案,請更新的問題與更多的信息

+0

對於遲到的回覆感到抱歉。我一直在擺弄服務的其他方面。但keyr,它的工作,數據正在反序列化。謝謝您的幫助 – jonniebigodes