1

This is my c# code:

  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]);


    }

and this is my javascript that gets the data from the api and sends it to the controller.

  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);
                                }
                            });

                        });
                    };   
                }

And this is what the data that the controller recieves looks like:

"[\"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]"

This is my model:

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

Why does it put \ in every string in the array? And how do i parse it to my model?

8
  • 1
    Did you try Json.Decode() to deserialize you data? Commented May 12, 2014 at 8:30
  • @DmytroTsiniavskyi no i haven't! Should it be in my c# code? Commented May 12, 2014 at 8:31
  • Did you copy paste that from the debugger? Because it displays string data as literal (that is, special characters escaped) – so "\"" is actually " Commented May 12, 2014 at 8:33
  • Yes, I would also recommend you to create JSON objects and to deserialize it using the JSON serialization methods in C# Commented May 12, 2014 at 8:33
  • Yes @marsze i copied it from the debugger. Commented May 12, 2014 at 8:34

1 Answer 1

3

I think this should help.

using System.Web.Helpers;    

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

Edits Well, I'm not sure that fully understand the structure of your model, but... Try the following:

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

More Edits Now I can spot the problem. Json.Decode() will not help you to create your Games object because data you send to controller is just stringnified array of strings and property names, which are required for converting are not provided. Maximum what you can do - deserialize this string to proper C# string array manually, and then do what you have already done - assign each property one by one. You can refactor it to look more pretty, create some extension method for string or even use reflection to loop through Games properties for assignment ... For example:

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

And then in your controller:

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

Also I have a question - What is MIN = Int32.Parse(playerstats[8])? Because it has a value of 37:20 (in example string you provided) and I'm not sure I understand what is this, because it will give the error during conversion to int.

Sign up to request clarification or add additional context in comments.

13 Comments

i get this error: system.web.controller.json(object) is a method which is not valid in the given context
@DanielGustafsson, I suggest you forgot to add necessary namespace: System.Web.Helpers
@DanielGustafsson, Have you tried to use namespace directly? Games gamestats = System.Web.Helpers.Json.Decode<Games>(playerstats); ?
That worked better! But now when i run it i get this error: SportsFantasy.Models.Games is not supported for deserialization of an array
@DanielGustafsson - try var stats = Json.Decode<string[]>(playerstats);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.