如何将我的字符串数组解析为我的c#模型?

时间:2014-05-12 08:26:07

标签: c# ajax json

这是我的c#代码:

  public void addBoxScore(string[] playerstats) 
    {
       Games gamestats = new Games();

       gamestats.GAME_ID = Int32.Parse(playerstats[0]);
       gamestats.TEAM_ID = Int32.Parse(playerstats[1]);
       gamestats.TEAM_ABBREVIATION = playerstats[2];
       gamestats.TEAM_CITY = playerstats[3];
       gamestats.PLAYER_ID = Int32.Parse(playerstats[4]);
       gamestats.PLAYER_NAME = playerstats[5];
       gamestats.START_POSITION = playerstats[6];
       gamestats.COMMENT = playerstats[7];

       gamestats.MIN = Int32.Parse(playerstats[8]);
       gamestats.FGM = Int32.Parse(playerstats[9]);
       gamestats.FGA = Int32.Parse(playerstats[10]);
       gamestats.FG_PCT = Int32.Parse(playerstats[11]);
       gamestats.FGTHREEM = Int32.Parse(playerstats[12]);
       gamestats.FGTHREEA = Int32.Parse(playerstats[13]);
       gamestats.FGTHREE_PCT = Int32.Parse(playerstats[14]);
       gamestats.FTM = Int32.Parse(playerstats[15]);
       gamestats.FTA = Int32.Parse(playerstats[16]);
       gamestats.FT_PCT = Int32.Parse(playerstats[17]);
       gamestats.OREB = Int32.Parse(playerstats[18]);
       gamestats.DREB = Int32.Parse(playerstats[19]);
       gamestats.REB = Int32.Parse(playerstats[20]);
       gamestats.AST = Int32.Parse(playerstats[21]);
       gamestats.STL = Int32.Parse(playerstats[22]);
       gamestats.BLK = Int32.Parse(playerstats[23]);
       gamestats.TO = Int32.Parse(playerstats[24]);
       gamestats.PF = Int32.Parse(playerstats[25]);
       gamestats.PTS = Int32.Parse(playerstats[26]);
       gamestats.PLUS_MINUS = Int32.Parse(playerstats[27]);


    }

这是我的javascript,它从api获取数据并将其发送给控制器。

  var date = "05/05/2014";

$.ajax({
    dataType: "jsonp",
    type: "post",
    crossDomain: true,
    url: 'http://stats.nba.com/stats/scoreboard/?LeagueID=00&gameDate=' + date + '&DayOffset=0',
    success: function (val) {
        var result = val.resultSets[0].rowSet;
        $.each(result, function (key, value) {
            var gameID = this[2];
            $.ajax({
                dataType: "jsonp",
                async: false,
                type: "post",
                crossDomain: true,
                gameID: { gameID: gameID },
                url: "http://stats.nba.com/stats/boxscore?GameID=" + gameID + "&RangeType=0&StartPeriod=0&EndPeriod=0&StartRange=0&EndRange=0",
                success: function (gameinfo) {
                    var w = gameinfo.resultSets[0].rowSet[0];
                    if (w[4] == "Final") {
                        var pstats = gameinfo.resultSets[4].rowSet;
                        $.each(pstats, function (key, value) {
                            var playerstats = this;
                            $.ajax({
                                async: false,
                                type: "post",
                                url: "/Stats/addBoxScore",
                                data: { playerstats: JSON.stringify(playerstats) },
                                done: function (data) {
                                    console.log(data);
                                },
                                error: function (jqXHR, err) {
                                    console.log(err);
                                }
                            });

                        });
                    };   
                }

这就是控制器收到的数据:

"[\"0041300201\",1610612764,\"WAS\",\"Washington\",2772,\"Trevor Ariza\",\"F\",\"\",\"37:20\",7,10,0.7,6,6,1,2,4,0.5,1,5,6,2,1,0,0,3,22,18]"

这是我的模特:

   public class Games
{
    [Key, Column(Order = 0)]
    public int GAME_ID { get; set; }
    public int TEAM_ID { get; set; }
    public string TEAM_ABBREVIATION { get; set; }
    public string TEAM_CITY { get; set; }
    [Key, Column(Order = 1)]
    public int PLAYER_ID { get; set; }
    public string PLAYER_NAME { get; set; }
    public string START_POSITION { get; set; }
    public string COMMENT { get; set; }
    public int MIN { get; set; }
    public int FGM { get; set; }
    public int FGA { get; set; }
    public int FG_PCT { get; set; }
    public int FGTHREEM { get; set; }
    public int FGTHREEA { get; set; }
    public int FGTHREE_PCT { get; set; }
    public int FTM { get; set; }
    public int FTA { get; set; }
    public int FT_PCT { get; set; }
    public int OREB { get; set; }
    public int DREB { get; set; }
    public int REB { get; set; }
    public int AST { get; set; }
    public int STL { get; set; }
    public int BLK { get; set; }
    public int TO { get; set; }
    public int PF { get; set; }
    public int PTS { get; set; }
    public int PLUS_MINUS { get; set; }

    public virtual Player player { get; set; } 
}

为什么在数组中的每个字符串中放置\?我如何解析它到我的模型?

1 个答案:

答案 0 :(得分:3)

我认为这应该有所帮助。

using System.Web.Helpers;    

public void addBoxScore(string playerstats) 
{
    Games gamestats = Json.Decode<Games>(playerstats);
}

<强>编辑 好吧,我不确定是否完全理解模型的结构,但是......尝试以下方法:

public void addBoxScore(string playerstats) 
{
    var gamestats = System.Web.Helpers.Json.Decode<IEnumerable<Games>>(playerstats);
}

更多编辑 现在我可以发现问题。 Json.Decode()无法帮助您创建Games对象,因为您发送给控制器的数据只是字符串的字符串和属性名称数组,不提供转换所需的数据。最大限度的工作 - 手动将此字符串反序列化为正确的C#字符串数组,然后执行您已完成的操作 - 逐个分配每个属性。您可以重构它以使其看起来更漂亮,为字符串创建一些扩展方法,甚至使用反射来循环遍历Games属性以进行赋值...例如:

public static class Extensions
    {
        public static Games ToGames(this string data)
        {
            var playerstats = data
                .Replace("[", string.Empty)
                .Replace("]", string.Empty)
                .Replace(@"\", string.Empty)
                .Replace("\"", string.Empty)
                .Split(',')
                .Select(s => s.Trim())
                .ToArray();


            var gamestats = new Games
            {
                GAME_ID = Int32.Parse(playerstats[0]),
                TEAM_ID = Int32.Parse(playerstats[1]),
                TEAM_ABBREVIATION = playerstats[2],
                TEAM_CITY = playerstats[3],
                PLAYER_ID = Int32.Parse(playerstats[4]),
                PLAYER_NAME = playerstats[5],
                START_POSITION = playerstats[6],
                COMMENT = playerstats[7],
                MIN = Int32.Parse(playerstats[8]),
                FGM = Int32.Parse(playerstats[9]),
                FGA = Int32.Parse(playerstats[10]),
                FG_PCT = Int32.Parse(playerstats[11]),
                FGTHREEM = Int32.Parse(playerstats[12]),
                FGTHREEA = Int32.Parse(playerstats[13]),
                FGTHREE_PCT = Int32.Parse(playerstats[14]),
                FTM = Int32.Parse(playerstats[15]),
                FTA = Int32.Parse(playerstats[16]),
                FT_PCT = Int32.Parse(playerstats[17]),
                OREB = Int32.Parse(playerstats[18]),
                DREB = Int32.Parse(playerstats[19]),
                REB = Int32.Parse(playerstats[20]),
                AST = Int32.Parse(playerstats[21]),
                STL = Int32.Parse(playerstats[22]),
                BLK = Int32.Parse(playerstats[23]),
                TO = Int32.Parse(playerstats[24]),
                PF = Int32.Parse(playerstats[25]),
                PTS = Int32.Parse(playerstats[26]),
                PLUS_MINUS = Int32.Parse(playerstats[27])
            };

            return gamestats;
        }
    }

然后在你的控制器中:

public void addBoxScore(string playerstats)
{
     Games result = playerstats.ToGames();
}

我也有一个问题 - 什么是MIN = Int32.Parse(playerstats[8])?因为它的值为37:20(在您提供的示例字符串中),我不确定我理解这是什么,因为它会在转换为int期间给出错误。