我“创建”了一个非常好的在线工具来为我创建一个C#类(http://json2csharp.com/),但现在我需要创建一个消费JSON的WebClient并使用他们各自的价值。WebClient使用JSON并发送属性值
其实我已经试图制作自己的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的代码兼容?
在此先感谢。
==== ==== UPDATE
我的观点:
@model DotaMix.Models.UserModel
@{
ViewBag.Title = @Model.personaname;
}
问题:标题是不可见的。
==== ==== UPDATE
我整个模型的代码:
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; }
}
}
您的代码似乎罚款。什么不起作用?你会得到什么错误? – I4V 2013-04-30 11:10:12
所以,他根本不起作用。似乎没有WebClient和属性之间的沟通。 – 2013-04-30 11:13:52
我真的没有得到你的问题。你给出的代码应该是有效的,它没有缺陷。 “让我的WebClient与JSON2Csharp代码兼容”意味着什么? – 2013-04-30 11:27:25