我正在使用.net的MVC模板,可以显示我的json数据,但现在我想将其解析成html格式,最好是以表格形式呈现,这样更美观。
我将HomeController.cs中获取的json数据传递给了About.cshtml视图,但它只是一个json字符串,所以看起来很糟糕。
public class HomeController : Controller
{
public JsonResult TestJson()
{
var client = new WebClient();
var response = client.DownloadString(new Uri("http://localhost:8080/projecten/api/leerlingen"));
var someObject = JsonConvert.DeserializeObject(response);
return new JsonResult() { Data = someObject, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
public ActionResult About()
{
var client = new WebClient();
var json = client.DownloadString(new Uri("http://localhost:8080/projecten/api/leerlingen"));
//parse json
ViewBag.Message = json;
return View();
}
}
这是JSON格式的数据
[{"inschrijvingsNummer":"0001","naam":"John Smith","email":"john.smith@example.com","evaluatieNummer":"270"},
{"inschrijvingsNummer":"0002","naam":"Joe Bloggs","email":"joe.bloggs@example.com","evaluatieNummer":"370"}]
在这个页面(About.cshtml)中展示了使用.NET转换为HTML的内容。请注意,保留了HTML标签。
@{
ViewBag.Title = "Evaluaties";
}
<h2>@ViewBag.Title.</h2>
<p>@ViewBag.Message</p>