如何使用JavaScriptSerializer反序列化json字符串

问题描述

我有一个JSON字符串,该字符串传递给httphandler,然后尝试使用JavaScriptSerializer反序列化。如果我使用像这样的简单JSON字符串,一切都很好

{"service":"WMS","datatype":"Tile","url":"http://localhost"}

但是,如果我这样使用JSON

{
    "source": {
        "service": "WMS","datatype": "Tile","url": "http://localhost","layer": "My Layer","zoomlevel": 4,"username": "User","password": "Password","tilesId": [
            15321,325,15332,9503,1429
        ],"aoi": {
            "type": "Polygon","coordinates": [
                [
                    [0,100],[8192,1200],3000],[0,1200]
                ]
            ]
        }
    },"output": {
        "type": "WFS","layer": "test_bbox ","username": "me","password": "Password1"
    }
}

我无法获取列表中的值。现在甚至连我从第一个短JSON字符串中获得的内容都丢失了。我试图用List作为源创建不同的类,但是它对我不起作用。 如何为此类JSON创建合适的类?

这是我使用的代码:

public class TestServer : IHttpHandler 
{
    public void ProcessRequest(HttpContext context)   
    {
        if (context.Request.QueryString["test"]!=null)
        {
            string input = context.Request.QueryString["test"];

            JavaScriptSerializer js = new JavaScriptSerializer();
            inputJson ex;

            
            ex = js.Deserialize<inputJson>(input);

            context.Response.Write("DataType:" + ex.datatype + "<br/>");
            context.Response.Write("Service: " + ex.service + "<br/>");
            context.Response.Write("Service Url: " + ex.url + "<br/>");


        }
    }


    public bool IsReusable => true;
}

public class inputeCore
{
    public List<Source> source { get; set; }
}

public class Source
{
    public List<inputJson> inputJson { get; set; }
    public string service { get; set; }
    public string datatype { get; set; }
    public string url { get; set; }
}

public class inputJson
{
    public string service { get; set; }
    public string datatype { get; set; }
    public string url { get; set; }     

} 

解决方法

您的课程应该是这样的:

    public class Aoi    {
        public string type { get; set; } 
        public List<List<List<int>>> coordinates { get; set; } 
    }

    public class Source    {
        public string service { get; set; } 
        public string datatype { get; set; } 
        public string url { get; set; } 
        public string layer { get; set; } 
        public int zoomlevel { get; set; } 
        public string username { get; set; } 
        public string password { get; set; } 
        public List<int> tilesId { get; set; } 
        public Aoi aoi { get; set; } 
    }

    public class Output    {
        public string type { get; set; } 
        public string url { get; set; } 
        public string layer { get; set; } 
        public string username { get; set; } 
        public string password { get; set; } 
    }

    public class Root    {
        public Source source { get; set; } 
        public Output output { get; set; } 
    }

您可以使用this将JSON转换为C#类。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...