0

I have a JSON string that I passing to httphandler and then I try to deserialize it by using JavaScriptSerializer. All works fine if I use simple JSON string like this

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

However, if I use JSON like this

{
    "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"
    }
}

I can't get values to a list. Even those which I was getting from the first short JSON string now are missing. I have tried to create different classes with List for source but it didn't work for me. How to create a proper class for such JSON?

This is my code which I using:

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

} 

1 Answer 1

2

Your class should be like this:

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

You can use this to convert your JSON to C# classes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.