2011-10-25 43 views
2

我有以下的JSON:錯誤反序列化JSON無法反序列化JSON對象轉換類型 'System.String'

{"workspace": { 
    "name":"Dallas", 
    "dataStores":"http://.....:8080/geoserver/rest/workspaces/Dallas/datastores.json", 
    "coverageStores":"http://.....:8080/geoserver/rest/workspaces/Dallas/coveragestores.json", 
    "wmsStores":"http://....:8080/geoserver/rest/workspaces/Dallas/wmsstores.json"}} 

而且我正嘗試反序列化INT這個類:

class objSON { 
     public string workspace { get; set; } 
     public string name { get; set; } 
     public string dataStores { get; set; } 
     public string coverageStores { get; set; } 
     public string wmsStores { get; set; }} 

objWS_JSON deserContWS = JsonConvert.DeserializeObject<objWS_JSON>(data); 
      var coberturas = deserContWS.coverageStores; 
      var almacenesDatos = deserContWS.dataStores; 
      var almacenesWMS = deserContWS.wmsStores; 
      var nombre = deserContWS.name; 

我得到以下錯誤:

無法將JSON對象反序列化爲類型'System.String'。

有什麼建議嗎?由於

回答

6

你的json對於你提供的類結構是不正確的。 json意味着name,dataStores,coverageStores和wmsSTore是工作空間類的子代。我想你想的階級結構是這樣的:

public class workspace 
{ 
    public string name { get; set; } 
    public string dataStores { get; set;} 
    public string coverageStores { get; set;} 
    public string wmsStores {get; set;} 
} 

public class objSON 
{ 
    public workspace workspace {get; set;} 
} 

嘗試,如果數據結構是不是你所追求的,那麼你需要改變你的JSON。

好吧我剛剛在示例應用程序中嘗試過,似乎工作正常。這裏是我使用的代碼:

class Program 
    { 
      static void Main(string[] args) 
      { 

       string str = @"{""workspace"": { 
        ""name"":""Dallas"", 
        ""dataStores"":""http://.....:8080/geoserver/rest/workspaces/Dallas/datastores.json"", 
        ""coverageStores"":""http://.....:8080/geoserver/rest/workspaces/Dallas/coveragestores.json  "", 
        ""wmsStores"":""http://....:8080/geoserver/rest/workspaces/Dallas/wmsstores.json""}}"; 

       var obj = JsonConvert.DeserializeObject<objSON>(str); 

    } 

} 

public class workspace 
{ 
    public string name { get; set; } 
    public string dataStores { get; set; } 
    public string coverageStores { get; set; } 
    public string wmsStores { get; set; } 
} 

public class objSON 
{ 
    public workspace workspace { get; set; } 
} 
+0

感謝您的回答我解決了錯誤,但我的元素爲空 – JMG

+0

您可以請你把你的序列化代碼在這裏 –

+0

objWS_JSON deserContWS = JsonConvert.DeserializeObject (data); var coberturas = deserContWS.coverageStores; var almacenesDatos = deserContWS.dataStores; var almacenesWMS = deserContWS.wmsStores; var nombre = deserContWS.name; – JMG

3

在JSON,workspace包含所有的休息,所以你應該是這樣的:

class Container { 
    public Workspace workspace { get; set; } 
} 

class Workspace { 
    public string name { get; set; } 
    public string dataStores { get; set; } 
    public string coverageStores { get; set; } 
    public string wmsStores { get; set; } 
} 

最起碼的JSON的結構相匹配 - 是否將工作或沒有是另一回事:)

+0

so'JsonConvert.DeserializeObject (data);'? –

+0

@羅伊納米爾:是的,我期望如此。 –

+0

謝謝,在數據中,我更改了我的JSON的斜線\/ – JMG

0

如果你看看JSON對象(如果你更清楚地規劃{}也許會更好),你會發現它試圖序列化所有的數據到workspace字段,而不是其他屬性。我希望你的對象看起來更像:

{ 
    "workspace": "whatever", 
    "name":"Dallas", 
    "dataStores":"http://.....:8080/geoserver/rest/workspaces/Dallas/datastores.json", 
    "coverageStores":"http://.....:8080/geoserver/rest/workspaces/Madrid/coveragestores.json", 
    "wmsStores":"http://....:8080/geoserver/rest/workspaces/Madrid/wmsstores.json" 
} 
+0

感謝您的編輯Moo果汁 – JMG