我想将 JSON 的结果存储在属性中,但我不知道如何。我将 ASP.NET 与 MVC 4/Razor 一起使用。
我的模型的构造函数是:
public UserModel()
{
WebClient request = new WebClient();
string response = request.DownloadString(url);
JObject _naturalUserObject = (JObject)JsonConvert.DeserializeObject(response);
}
我有以下属性:
[Key]
public int Id { get; set; }
public int DisplayName { get; set; }
public int Avatar { get; set; }
要从我的 JSON 中访问一个简单的值:
_naturalUserObject["users"][0]["name"];
最后一个问题是:如何将 JObject 中的每个值存储在模型的属性中并最终将其显示到我的视图中?
提前致谢。
/EDIT/ 我现在的模型是:
public UserModel()
{
WebClient request = new WebClient();
string response = request.DownloadString(String.Format("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={0}&steamids={1}", ConfigurationManager.AppSettings["SteamApiKey"].ToString(), HttpContext.Current.Request.Cookies["SteamIdCookie"].Value));
string _naturalUserObject = JsonConvert.DeserializeObject<string>(response);
}
我的财产之一是:
private string _avatar;
public string Avatar
{
get { return _avatar; }
set { _avatar = _naturalUserObject["response"]["players"][0]["avatar"]; }
}
但没有成功。=(