2013-04-30 33 views
2

我“创建”了一个非常好的在线工具来为我创建一个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; } 
    } 
} 
+0

您的代码似乎罚款。什么不起作用?你会得到什么错误? – I4V 2013-04-30 11:10:12

+0

所以,他根本不起作用。似乎没有WebClient和属性之间的沟通。 – 2013-04-30 11:13:52

+0

我真的没有得到你的问题。你给出的代码应该是有效的,它没有缺陷。 “让我的WebClient与JSON2Csharp代码兼容”意味着什么? – 2013-04-30 11:27:25

回答

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真的返回超过10分的球员,你可以做这样的:

控制器:

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> 
} 
+0

这正是我想要的!哦,狮子座,非常感谢! – 2013-04-30 16:46:35

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; } 
    } 
} 
+0

你好,卡洛斯。谢谢你的贡献。所以,我试过这个:'[JsonProperty(“personaname”)] public string personaname {get;组; }'。这是你的意思吗? – 2013-04-30 11:42:06

+0

是的。究竟。 NewtonSoft使用该标记来填充类属性,并使用json响应 – 2013-04-30 11:46:19

+0

Ok。我做到了,但没有成功。 =( – 2013-04-30 11:47:14