3

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

3 Answers 3

7

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; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer I solve the error but my elements are null
objWS_JSON deserContWS = JsonConvert.DeserializeObject<objWS_JSON>(data); var coberturas = deserContWS.coverageStores; var almacenesDatos = deserContWS.dataStores; var almacenesWMS = deserContWS.wmsStores; var nombre = deserContWS.name;
Sorry Kevin I was failed with the deserialization I forget change, now is working!. Thanks
3

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

5 Comments

so JsonConvert.DeserializeObject<Container>(data); ?
@RoyiNamir: Yup, I'd expect so.
Thanks, in data I change the slash \ of my JSON by this /
Yes, because data is the JSON clean whithout backslashes
@JMG: So where do the backslashes come into it at all? It's unclear why you've suddenly brought them up... you shouldn't need to replace anything.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.