Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Also you can integrate the free <a href="http://james.newtonking.com/pages/json-net.aspx" rel="noreferrer">Json.NET</a> library within your code.</p> <p>This library does not suffer the problems <code>JavascriptSerializer</code> has like the circular reference problem. </p> <p>This is a sample using the library to output JSON from a controller action</p> <pre><code>public virtual ActionResult ListData() { Dictionary&lt;string, string&gt; openWith = new Dictionary&lt;string, string&gt;(); 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; } </code></pre> <p>If you execute this action you will get the following results</p> <pre><code>{ "txt": "notepad.exe", "bmp": "paint.exe", "dib": "paint.exe", "rtf": "wordpad.exe" } </code></pre> <p>JsonNetResult is a simple custom wrapper class around the functionalities of the Json.NET library.</p> <pre><code>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(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload