我“创建”了一个非常好的在线工具来为我创建一个 C# 类 ( http://json2csharp.com/ ),但现在我需要创建一个使用 JSON 并使用它们各自的值填充属性的 WebClient。
实际上我已经尝试过制作自己的WebClient,但我不知道如何将他实现为从JSON2Csharp生成的代码。
JSON2CSharp 代码:
public class UserModel
{
public int communityvisibilitystate { get; set; }
public int profilestate { get; set; }
public string personaname { get; set; }
public int lastlogoff { get; set; }
public string profileurl { get; set; }
public string avatar { get; set; }
public string avatarmedium { get; set; }
public string avatarfull { get; set; }
public int personastate { get; set; }
}
public class Response
{
public List<UserModel> users { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}
现在,我尝试使用 UserModel 的类来做到这一点:
public class UserModel
{
public UserModel()
{
WebClient request = new WebClient();
var response = request.DownloadString(url);
JsonConvert.PopulateObject(response, this);
}
...
}
但没有成功(正如预期的那样)。所以我问:我该怎么做才能使我的 WebClient 与 JSON2Csharp 的代码兼容?
提前致谢。
==== 更新 ====
我的观点:
@model DotaMix.Models.UserModel
@{
ViewBag.Title = @Model.personaname;
}
问题:标题是不可见的。
==== 更新 ====
我的完整模型代码:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Globalization;
using System.Web;
using System.Web.Security;
using System.Net;
using System.Configuration;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ServiceStack.Text;
namespace Mix.Models
{
public class UserModel
{
public UserModel()
{
WebClient request = new WebClient();
var response = request.DownloadString(url);
JsonConvert.PopulateObject(response, this);
}
public string steamid { get; set; }
public int communityvisibilitystate { get; set; }
public int profilestate { get; set; }
public string personaname { get; set; }
public int lastlogoff { get; set; }
public string profileurl { get; set; }
public string avatar { get; set; }
public string avatarmedium { get; set; }
public string avatarfull { get; set; }
public int personastate { get; set; }
}
public class Response
{
public List<UserModel> users { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}
}