解序列化JSON时出错解决方法无法将JSON对象反序列化为类型“System.String” [英] Error deserializing JSON Cannot deserialize JSON object into type 'System.String'

查看:3434
本文介绍了解序列化JSON时出错解决方法无法将JSON对象反序列化为类型“System.String”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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"}}

我试图反序列化int这个类:

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;

我收到以下错误:

无法将JSON对象反序列化为类型System.String。

任何想法?感谢

推荐答案

您的json对您提供的类结构不正确。 json意味着name,dataStores,coverageStores和wmsSTores是工作空间类的子级。我想你想要的类结构是这样的:

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

尝试,如果那个数据结构不是你的那么你需要改变你的json。

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

Ok我刚刚在一个示例应用程序中尝试,似乎工作正常。这是我使用的代码:

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

这篇关于解序列化JSON时出错解决方法无法将JSON对象反序列化为类型“System.String”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆