0

I'm using a 3rd party map for some charting functionality within our intranet. To populate the regions, I need to pass it an Object Collection (javascript) formatted thusly:

            simplemaps_usmap_mapdata.state_specific = {
            AZ: {
                name: "Arizona",
                description: "The best state in the whole got damn union",
                color: "#cecece",
                hover_color: "default",
                url: "",
              },
            NH: {
                name: "New Hampshire",
                description: "Small and insignificant",
                color: "#f68831",
                hover_color: "default",
                url: "",
              }
        }

Obviously, it'd be much better if it were an array of objects, then it'd be simple enough to return via my C# controller a List, but since it's an object collection, what is the best way to return JSON formatted this way via my C# controller?

1
  • Could you produce a [Data Contract] for each State object, serialise these to JSON and then manually format the rest of the JSON? I'm not sure of a way to automatically convert an array of objects with custom structure like that. I can put together an example for this if you think this would help. Commented Jun 8, 2017 at 16:24

2 Answers 2

2

First, a class:

class MyClass {
    public string name {get;set;}
    public string description {get;set;}
    public string color {get;set;}
    public string hover_color {get;set;}
    public string url {get;set;}
}

Then, a dictionary:

var result = new Dictionary<string, MyClass>();

Add to the dictionary:

result["AZ"] = new MyClass { name="xyz"... };

Finally, return the dictionary via the JSON method:

return JSON(result);
Sign up to request clarification or add additional context in comments.

4 Comments

didn't even think about a dictionary, I rather like that
Creating the intermediate class goes a long way. Inevitably, there will be another method that needs the exact same structure as an output ;)
yeah I didn't show creating the intermediate class in my answer, but wrapping it as a dictionary vs using dynamic I think is a much better route
Thank you, I didn't even think of the Dictionary either... Exactly what I needed.
0

I am going to assume you have already configured your controller to return JSON, meaning your object type is the only thing that needs to be setup.

The problem with this format, as you are likely already aware of, is that you will need to define an object that has all of the states on it's root level. This is tedious and likely overkill when you don't need every single state to be returned.

To solve this, utilize either the dynamic type or an anonymous object in C#. This will allow you to define just what you need at that time:

Pseudocode for your controller method:

public dynamic MyAction() // Dynamic
public object MyAction() // Object
{
    return new {
        AZ = new StateObj(), // Insert your state type here
        NH = new StateObj()
    }
}

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.