11

Hi Im trying to send a string to a view that looks like json.

Im sending a list of places:

class Place 
        {
            public string title { get; set; }
            public string description { get; set; }
            public double latitude { get; set; }
            public double longitude { get; set; }
        }

List<Place> placeList = new List<Place>(); 
//add places to PlaceList

//Then i do this
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string sJSON = oSerializer.Serialize(placeList);
            ViewBag.Places = sJSON;

In the view its rendering output like this though:

[{&quot;title&quot;:&quot;sdf sdfsd sdf sd f&quot;,&quot;description&quot;:&quot;sdf sdf sd fsd sd sdf sdf dssd sdf sd s&quot;,&quot;latitude&quot;:53.740259851464685,&quot;longitude&quot;:-2.4602634343627927},

How do i get it to render as normal json in the view? minus &quot;etc?

3 Answers 3

20

In your comment below you say your view is using @ViewBag.Places

Are you using Razor? If so the @ syntax does the same thing as <%: - it encodes the content.

Use the IHtmlString interface to avoid it, so either:

ViewBag.Places = new HtmlString(sJSON);

Or

@HtmlString(ViewBag.Places)
Sign up to request clarification or add additional context in comments.

Comments

6
@Html.Raw(ViewBag.Places)

also works

Comments

1

did you try ?

string sJSON = HttpServerUtility.HmltDecode(oSerializer.Serialize(placeList));

4 Comments

also I think that it is strange that oSerializer.Serialize return html encoded string. As you sure that the rendering in your view doesn't have any pre-treatment ?
I tried: HttpUtility.HtmlDecode(oSerializer.Serialize(placeList)); - same result
in my view i have: "places": @ViewBag.Places
so did you try <%= HttpUtility.HtmlDecode(ViewBag.Places) %> ? (I don't use razor)

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.