如何使用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; }     

} 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)