我有以下 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"}}
我正在尝试在此类中反序列化:
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”类型。
有任何想法吗?谢谢
您的 json 对于您提供的类结构不正确。 json 暗示 name、dataStores、coverageStores 和 wmsSTores 是工作区类的子级。我认为你想要的类结构是这样的:
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; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)