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

谁说我不能喝 提交于 2019-12-10 19:35:57

问题


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


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



回答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 :)




回答3:


If you look at the JSON object (it is perhaps better if you laid out your { and } a little more clearly), you'll see that it is trying to serialize all that data in to the workspace field, and not the other properties. I would expect your object to look something more like:

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


来源:https://stackoverflow.com/questions/7891256/error-deserializing-json-cannot-deserialize-json-object-into-type-system-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!