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

how to Deserialize json string using JavaScriptSerializer

我有一个 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], 
                    [8192, 3000], 
                    [0, 3000], 
                    [0, 1200]
                ]
            ]
        }
    },
    "output": {
        "type": "WFS",
        "url": "http://localhost",
        "layer": "test_bbox ",
        "username": "me",
        "password": "Password1"
    }
}

我无法获取列表的值。即使是我从第一个短 JSON 字符串中得到的那些现在也丢失了。我曾尝试使用源列表创建不同的 classes,但它对我不起作用。 如何为这样的 JSON 创建一个合适的 class?

这是我使用的代码:

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

} 

你的class应该是这样的:

    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# classes。