MVC4 - 鉴于显示JSON结果属性 [英] MVC4 - Displaying JSON result properties in view
本文介绍了MVC4 - 鉴于显示JSON结果属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先,我是新来的MVC。
我要显示在HTML视图JSON响应的属性。
例如,我想获得页面的数量从喜欢一个页面上的JSON响应和显示只是喜欢的数量。
任何帮助很多AP preciated:)
//
// GET:/ Facebook的/ 公众的ActionResult指数()
{
VAR JSON =新的WebClient()DownloadString(https://graph.facebook.com/google);
JsonConvert.DeserializeObject< RootObject>(JSON); 返回视图();
} 公共类所属分类
{
公共字符串ID {搞定;组; }
公共字符串名称{;组; }
} 公共类位置
{
公共字符串街{搞定;组; }
公共字符串城市{搞定;组; }
公共字符串状态{搞定;组; }
公共字符串国{搞定;组; }
公共字符串拉链{搞定;组; }
公共双纬度{搞定;组; }
公共双经度{搞定;组; }
} 公共类封面
{
公共字符串cover_id {搞定;组; }
公共字符串源{搞定;组; }
公众诠释offset_y {搞定;组; }
公众诠释offset_x {搞定;组; }
} 公共类RootObject
{
关于{获得公共字符串;组; }
公共字符串奖{搞定;组; }
公共字符串类{搞定;组; }
公开名单<所属分类> category_list {搞定;组; }
公众诠释签入{搞定;组; }
公共字符串COMPANY_OVERVIEW {搞定;组; }
公共字符串描述{搞定;组; }
公共字符串创建{搞定;组; }
公共BOOL is_published {搞定;组; }
公共场所的位置{搞定;组; }
公共字符串任务{搞定;组; }
公共字符串电话{搞定;组; }
公共字符串产品{搞定;组; }
公众诠释talking_about_count {搞定;组; }
公共字符串的用户名{获得;组; }
公共字符串网站{搞定;组; }
公众诠释were_here_count {搞定;组; }
公共字符串ID {搞定;组; }
公共字符串名称{;组; }
公共字符串链接{搞定;组; }
公众诠释喜欢{搞定;组; }
公共盖盖{搞定;组; }
}
}
}
解决方案
您应该采取行动的对象传递给视图:
公众的ActionResult指数()
{
VAR JSON =新的WebClient()DownloadString(https://graph.facebook.com/google);
VAR根= JsonConvert.DeserializeObject< RootObject>(JSON); 返回视图(根);
}
,然后在您的视图可以显示任何你想要的属性:
@Model RootObject
< HTML和GT;
< HEAD>
<标题>显示性能< /标题>
< /头>
<身体GT;
@ Model.likes喜欢。
< /身体GT;
< / HTML>
这是如果你使用的剃刀语法。
First of all, I'm new to MVC.
I want to display the properties of the JSON response in a html view.
For example, i want to get the number of page likes from the JSON response and display just the number of likes on a page.
Any help is much appreciated :)
//
// GET: /Facebook/
public ActionResult Index()
{
var json = new WebClient().DownloadString("https://graph.facebook.com/google");
JsonConvert.DeserializeObject<RootObject>(json);
return view();
}
public class CategoryList
{
public string id { get; set; }
public string name { get; set; }
}
public class Location
{
public string street { get; set; }
public string city { get; set; }
public string state { get; set; }
public string country { get; set; }
public string zip { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
}
public class Cover
{
public string cover_id { get; set; }
public string source { get; set; }
public int offset_y { get; set; }
public int offset_x { get; set; }
}
public class RootObject
{
public string about { get; set; }
public string awards { get; set; }
public string category { get; set; }
public List<CategoryList> category_list { get; set; }
public int checkins { get; set; }
public string company_overview { get; set; }
public string description { get; set; }
public string founded { get; set; }
public bool is_published { get; set; }
public Location location { get; set; }
public string mission { get; set; }
public string phone { get; set; }
public string products { get; set; }
public int talking_about_count { get; set; }
public string username { get; set; }
public string website { get; set; }
public int were_here_count { get; set; }
public string id { get; set; }
public string name { get; set; }
public string link { get; set; }
public int likes { get; set; }
public Cover cover { get; set; }
}
}
}
解决方案
Your action should pass the object to the view:
public ActionResult Index()
{
var json = new WebClient().DownloadString("https://graph.facebook.com/google");
var root=JsonConvert.DeserializeObject<RootObject>(json);
return view(root);
}
and then in your view you can show whichever property you want:
@Model RootObject
<html>
<head>
<title>Showing properties</title>
</head>
<body>
@Model.likes likes.
</body>
</html>
This is if you use the Razor syntax.
这篇关于MVC4 - 鉴于显示JSON结果属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文