3

I have this result

http://screencast.com/t/s1loWQQ6wZG

https://content.screencast.com/users/levalencia/folders/Jing/media/8c8f0201-4a75-4f91-b311-6ed64c382fac/2015-05-22_1932.png

My code is as follows:

What am I missing so that the JSON is prettified?

View

@model PruebasAD.Models.SuccessViewModel
@{
    ViewBag.Title = "TestRestCall";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>TestRestCall</h2>
<article>
    <aside class="green">
        @Model.Message
    </aside>
    <aside>
        <pre id="json-result">
        </pre>
    </aside>
</article>


<script type="text/javascript">
    $(document).ready(function(){
        var str = JSON.stringify(@(new MvcHtmlString(Model.JSON)), undefined, 2); // indentation level = 2
        $('#json-result').html(str);
        console.log(str);
    });
</script>

controller action

public async Task<ActionResult> TestRestCall()
        {
            Uri serviceRoot = new Uri(azureAdGraphApiEndPoint);
            var token = await GetAppTokenAsync();
            string requestUrl = "https://graph.windows.net/mysaasapp.onmicrosoft.com/users?api-version=2013-04-05";

            HttpClient hc = new HttpClient();
            hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
                "Bearer", token);

            HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));

            if (hrm.IsSuccessStatusCode)
            {
                string jsonresult = await hrm.Content.ReadAsStringAsync();
                return View("TestRestCall", new SuccessViewModel
                {
                    Name = "The Title",
                    Message = "The message",
                    JSON = jsonresult.ToJson()
                });
            }
            else
            {
                return View();
            }

        }

and the model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

    namespace PruebasAD.Models
    {
        public class SuccessViewModel
        {
            public string Name { get; set; }
            public string Message { get; set; }
            public string JSON { get; set; }
        }
    }
1
  • what does the rendered html? suspect 1st argument to stringify isnt a javascript object Commented May 24, 2015 at 0:09

1 Answer 1

1

First you need to convert your Model in a javascript object using JSON.parse, then pass to JSON.stringify

see documentation for json.parse

<script type="text/javascript">
$(document).ready(function(){
    var myObject = JSON.parse(@Html.Raw(new MvcHtmlString(Model.JSON)));
    var str = JSON.stringify(myObject,undefined, 2); // indentation level = 2
    $('#json-result').html(str);
    console.log(str);
});
Sign up to request clarification or add additional context in comments.

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.