问题
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