9

I've got a dictionary<string,string> as part of my view model. What I'm trying to do is cycle this object and output it as a json object. My reason for this is so I can localise my client script files correctly.

The output for this needs to look something like

var clientStrings = {"test":"yay","goodBye":"Nah"};

Any ideas how to achieve this correctly.

Thanks in advance.

3 Answers 3

15

It's built into MVC. Just return Json(yourobject).

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

Comments

10

Considering you are on mvc 3 you'll have access to JavaScriptSerializer. You should be able to do the following:

JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize((object)yourDictionary);

This will serialize your dictionary to json. You may want to do this in the controller before sending the ViewData to the view to render.

Comments

8

Also you can integrate the free Json.NET library within your code.

This library does not suffer the problems JavascriptSerializer has like the circular reference problem.

This is a sample using the library to output JSON from a controller action

public virtual ActionResult ListData() {
    Dictionary<string, string> openWith = new Dictionary<string, string>();
    openWith.Add( "txt", "notepad.exe" );
    openWith.Add( "bmp", "paint.exe" );
    openWith.Add( "dib", "paint.exe" );
    openWith.Add( "rtf", "wordpad.exe" );

    JsonNetResult jsonNetResult = new JsonNetResult();
    jsonNetResult.Formatting = Formatting.Indented;
    jsonNetResult.Data = openWith;
    return jsonNetResult;
}

If you execute this action you will get the following results

{
  "txt": "notepad.exe",
  "bmp": "paint.exe",
  "dib": "paint.exe",
  "rtf": "wordpad.exe"
}

JsonNetResult is a simple custom wrapper class around the functionalities of the Json.NET library.

public class JsonNetResult : ActionResult
{
    public Encoding ContentEncoding { get; set; }
    public string ContentType { get; set; }
    public object Data { get; set; }

    public JsonSerializerSettings SerializerSettings { get; set; }
    public Formatting Formatting { get; set; }

    public JsonNetResult() {
        SerializerSettings = new JsonSerializerSettings();
    }

    public override void ExecuteResult( ControllerContext context ) {
        if ( context == null )
            throw new ArgumentNullException( "context" );

        HttpResponseBase response = context.HttpContext.Response;

        response.ContentType = !string.IsNullOrEmpty( ContentType )
            ? ContentType
            : "application/json";

        if ( ContentEncoding != null )
            response.ContentEncoding = ContentEncoding;

        if ( Data != null ) {
            JsonTextWriter writer = new JsonTextWriter( response.Output ) { Formatting = Formatting };

            JsonSerializer serializer = JsonSerializer.Create( SerializerSettings );
            serializer.Serialize( writer, Data );

            writer.Flush();
        }
    }
}

6 Comments

I'm not sure JSON.NET is able to do Dictionaries, can you confirm that it can, as that is what RubbleFord is asking
It works seamlessly :) I have edited my answer for your pleasure
@Lorenzo, why use this over the built in Json(xxx)? Any advantages?
@Adam Tuliper: I cant confirm it for sure, but I think that internally Json(xxx) uses JavaScriptSerializer which suffers of the circular reference problem which you can read about it here and here. My answer is just an addendum to the accepted answer which is 100% correct
@AdamTuliper: JSON.NET has the possibility of whitelisting which properties you want to output via the JsonProperty attribute, see stackoverflow.com/questions/2546138/…. Additionally, JSON.NET can serialize a property having a getter but no setter, which the WCF DataContract JSON serializer can't.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.