2016-12-02 77 views
3

我一直在閱讀有關反序列化json的線程,並嘗試了很多方法來實現它,但似乎無法完成此工作。 這是我到目前爲止已經完成:類型System.String不受數組支持

public class oVesselMovement : oVMLocation, IWPConditions, IVessel, IStatus  
{ 
    public int wpID { get; set; } 
    public string charts { get; set; } 
    public string latNS { get; set; } 
    public string longEW { get; set; } 
    public string course { get; set; } 
    public double toNextWPNM { get; set; } 
    public double toGoNM { get; set; } 
    public string eda { get; set; } 
    public string eta { get; set; } 
} 

根類

public class RootVM 
{ 
    public List<oVesselMovement> jsnObj { get; set; } 
} 

JSON

{"jsnObj":[{"vmID":"1","charts":"2111","latNS":"10°29.10 N","longEW":"123°25.83 E","course":"420°","toNextWPNM":0,"toGoNM":"27","eda":"12-15-2016","eta":"11:01"},{"vmID":"2","charts":"2211","latNS":"11°29.10 N","longEW":"124°25.83 E","course":"420°","toNextWPNM":0,"toGoNM":"27","eda":"12-15-2016","eta":"11:01"}]} 

反序列化

[WebMethod] 
public void saveVMDT(string jsnObj) 
{ 
    RootVM rootObj = new JavaScriptSerializer().Deserialize<RootVM>(jsnObj); 
    foreach (var obj in rootObj.jsnObj) 
    { 
     try 
     { 
      ... 
    } 

我也試着轉換List<oVesselMovement> jsnObj { get; set; }oVesselMovement[] jsnObj但無濟於事。

我不斷收到此錯誤:

"{"Message":"Type \u0027System.String\u0027 is not supported for deserialization of an array."....

任何幫助,將不勝感激。

+0

請嘗試[this](http://stackoverflow.com/a/25654220/5588347)。 –

+0

使用Json.net。我試過它的代碼它的作品 – esiprogrammer

回答

0

toGoNM是一個字符串,而不是數字。你需要使用正確的類型,修正它在C#端或JSON端。您在C#類中有wpID,但在JSON中有vmID;再次,錯誤的類型。

0

我不確定JavaScriptSerializer在內部做了些什麼,但是newtonsoft的JToken處理了這個問題。下面的代碼工作:

public class oVesselMovement //notice no ancestor class or interfaces 
    { 
     public int wpID { get; set; } 
     public string charts { get; set; } 
     public string latNS { get; set; } 
     public string longEW { get; set; } 
     public string course { get; set; } 
     public double toNextWPNM { get; set; } 
     public double toGoNM { get; set; } 
     public string eda { get; set; } 
     public string eta { get; set; } 
    } 

    public class RootVM 
    { 
     public List<oVesselMovement> jsnObj { get; set; } 
    } 

    public class MakeItSo 
    { 
     public const string json = "{\"jsnObj\":[{\"vmID\":\"1\",\"charts\":\"2111\",\"latNS\":\"10°29.10 N\",\"longEW\":\"123°25.83 E\",\"course\":\"420°\",\"toNextWPNM\":0,\"toGoNM\":\"27\",\"eda\":\"12-15-2016\",\"eta\":\"11:01\"},{\"vmID\":\"2\",\"charts\":\"2211\",\"latNS\":\"11°29.10 N\",\"longEW\":\"124°25.83 E\",\"course\":\"420°\",\"toNextWPNM\":0,\"toGoNM\":\"27\",\"eda\":\"12-15-2016\",\"eta\":\"11:01\"}]}"; 

     public void SaveVmd() 
     { 
      RootVM rootObj = JToken.Parse(json).ToObject<RootVM>(); 
      Console.WriteLine($"Parsed {rootObj.jsnObj.Count} vessel movement objects"); 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      var makeitso = new MakeItSo(); 
      makeitso.SaveVmd(); 
     } 
    } 

不過請注意,我沒有oVesselMovement作爲oVMLocation的祖先或任何接口的實現者。我相信問題可能出在oVMLocation上的字段或屬性中,而這些字段或屬性不在json中。

相關問題