我“创建”了一个非常好的在线工具来为我创建一个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; }
}
}
答案 0 :(得分:3)
首先,请查看有关如何执行所需操作的完整代码:http://pastebin.com/aQNtjLHs
您获得的JSON可以返回超过1个玩家。如果您确定它只返回1,您可以像这样使用:
控制器:
public ActionResult Index()
{
WebClient request = new WebClient();
var url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/? key=B88B7699C6EACAE138DDB93BED6467EC&steamids=76561198026602449";
var response = request.DownloadString(url);
var myObject = new RootObject();
JsonConvert.PopulateObject(response, myObject);
//get only the first player
return View(myObject.response.players.FirstOrDefault());
}
查看:
@model DotaMix.Models.Player
@Model.personaname
现在,如果那个json真的返回超过1个玩家,你可以这样做:
控制器:
public ActionResult Index()
{
WebClient request = new WebClient();
var url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=B88B7699C6EACAE138DDB93BED6467EC&steamids=76561198026602449";
var response = request.DownloadString(url);
var myObject = new RootObject();
JsonConvert.PopulateObject(response, myObject);
return View(myObject.response);
}
查看:
@model DotaMix.Models.Response
@foreach (var player in Model.players)
{
<li>@player.personaname</li>
}
答案 1 :(得分:0)
我见过你正在使用Newtonsoft程序集。您是否尝试过包含JsonProperty定义?。
[JsonProperty("TheProperty")]
检查此示例类:
namespace Json.CareerJsonTypes
{
internal class Act13
{
[JsonProperty("completed")]
public bool Completed { get; set; }
[JsonProperty("completedQuests")]
public IList<CompletedQuest9> CompletedQuests { get; set; }
}
}