3
asp.net的MVC頁
對我有這樣的結果JSON字符串化工作不
我的代碼如下:
我缺少什麼,這樣的JSON是美化?
查看
@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>
控制器動作
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();
}
}
,你需要你的模型轉換使用JSON.parse JavaScript對象模型
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; }
}
}
呈現的html是什麼?可疑的第一個參數stringify不是一個JavaScript對象 – JJS