IIS7 Rewrite
IIS7 Rewrite

Reputation: 799

Object definition in C# to comply with JSON definition

I have this JSON definition:

{
    "id": 1400,
    "types": {
        "type one": {
           "url": "http://www.example.com",
           "desc": "type one desc"
        },
        "type two": {
            "url": "http://www.example.com",
            "desc": "type two desc"
        }
    }
}

I need to create a C# class that when serialized produces the JSON above.

The problem I'm having is with "type one" and "type two". If my class looks like this:

public class mytypes{

    public int id { get; set; }
    public List<mytype> types { get; set; }
}

where mytype is:

public class mytype {

    public string url { get; set; }
    public string desc { get; set; }
}

Data is coming from a database and this generates an array of "types", not one "types" object with objects inside it that have a description as the definition (type one, type two).

How can I change my class to generate "type one" and "type two" inside of "types", and not an array?

Upvotes: 0

Views: 326

Answers (1)

Piotr Stapp
Piotr Stapp

Reputation: 19820

Instead of using:

public List<mytype> types { get; set; }

you have to use Dictionary so the property will be:

public Dictionary<string,mytype> types { get; set; }

Upvotes: 1

Related Questions