2

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

2 回答 2

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>
}
于 2013-04-30T14:14:20.227 回答
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; }
    }
}
于 2013-04-30T11:20:56.220 回答