Error deserializing JSON Cannot deserialize JSON object into type 'System.String'

11,313

Solution 1

your json is incorrect for the class structure you've provided. The json implies that name, dataStores, coverageStores and wmsSTores are children of a workspace class. I think the class structure you want is this:

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

try that, if that data structure is not what you are after then you need to change your json.

Ok I've just tried in a sample app and seems to work fine. Here is the code I used:

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

Solution 2

In the JSON, workspace contains all the rest, so you should have something like:

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

At the very least that matches the structure of the JSON - whether it'll work or not is another matter :)

Share:
11,313
John Gilbert
Author by

John Gilbert

Updated on June 05, 2022

Comments

  • John Gilbert
    John Gilbert almost 2 years

    I have the following JSON:

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

    And I´m trying to deserialize int this class:

     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;
    

    And I get the following error:

    Cannot deserialize JSON object into type 'System.String'.

    Any ideas? Thanks