Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert .Net object to JSON object in the view

I want to convert a .Net object in to JSON in the view. My view model is like this,

public class ViewModel{     public SearchResult SearchResult { get; set;}     }      public class SearchResult {     public int Id { get; set; }     public string Text{ get; set; } } 

I want to convert Model.SearchResult in to a JSON object. Currenty I'm doing it like this:

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); //.... var s = @serializer.Serialize(Model.Institution); 

but the result is like this,

var s = { "Name":"a","Id":1}; Create:228Uncaught SyntaxError: Unexpected token & 

How can I convert this correctly in to a JSON object?

like image 422
Jayantha Lal Sirisena Avatar asked Dec 14 '11 09:12

Jayantha Lal Sirisena


People also ask

Can we convert list to JSON in C#?

In this article, we will explore how to convert List Object to JSON String in ASP.Net using C# with an example and sample code. In this example, we have created UserInfo class to Serialize and De-serialize. When we click on Serialize button, Object will convert to JSON String format .


2 Answers

Try using this method:

@Html.Raw(Json.Encode(Model.Content))

like image 76
Kyeotic Avatar answered Sep 25 '22 10:09

Kyeotic


I use this helper since asp.net mvc 2

public static MvcHtmlString ToJson(this HtmlHelper html, object obj) {   JavaScriptSerializer serializer = new JavaScriptSerializer();   return MvcHtmlString.Create(serializer.Serialize(obj)); }  public static MvcHtmlString ToJson(this HtmlHelper html, object obj, int recursionDepth) {   JavaScriptSerializer serializer = new JavaScriptSerializer();   serializer.RecursionLimit = recursionDepth;   return MvcHtmlString.Create(serializer.Serialize(obj)); } 

And in the view:

  <script>     var s = @(Html.ToJson(Model.Content));   </script> 

I should replace serializer with the JSON.Encode(..) now, like mentionned in the refer by Hemant. (It use itself JavaScriptSerializer).

The source of your problem is the "@" which HTML encode the JSON. You can use @Html.Raw(..) to avoid this behavior.

+: take a look for Json.Net http://json.codeplex.com/

JSON.Net update

I've updated the helper a while ago with JSON.net (much better).

It seems some users continue to read, upvote and use the old code. I'd like they use a better way, with the new version below, or by using NGon like Matthew Nichols has noticed it in a comment.

Here's the code:

using System; using Newtonsoft.Json;  namespace System.Web.Mvc {   public static class HtmlHelperExtensions   {     private static readonly JsonSerializerSettings settings;      static HtmlHelperExtensions()     {       settings = new JsonSerializerSettings();       // CamelCase: "MyProperty" will become "myProperty"       settings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();     }      public static MvcHtmlString ToJson(this HtmlHelper html, object value)     {       return MvcHtmlString.Create(JsonConvert.SerializeObject(value, Formatting.None, settings));     }   } } 
like image 33
Loic El Thesea Avatar answered Sep 22 '22 10:09

Loic El Thesea