Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Null object when sending json.net to a asp.net MVC

Discussion in 'Scripting' started by BigFI, Oct 9, 2015.

  1. BigFI

    BigFI

    Joined:
    Dec 14, 2014
    Posts:
    9
    Hello

    I'm using an asp.net MVC webapp to store my leaderboard and other things.

    I have a first part where I download a list of scores from my MVC. The MVC send it as a JSON text. In Unity, I use JSON.net to convert it to a list of object. No problem here.

    In a second part, I want to upload new scores on my MVC. And this is where I have a problem. I tried a lot of configuration and everytime, my MVC method receive a null object. I manage to make it receive an object with null propoerty, but I can't reproduce that.

    Here is the method in my MVC :

    Code (CSharp):
    1.  
    2. [HttpPost]
    3.         public JsonResult PostScore(Score score)
    4.         {
    5.             try {
    6.                 score.scoreID = db.Scores.Max(S => S.scoreID) + 1;
    7.                 db.Scores.Add(score);
    8.                 db.SaveChanges();
    9.             }
    10.             catch (Exception e)
    11.             {
    12.                 return Json(e.Message);
    13.             }
    14.             return Json("OK");
    15.         }
    16.  
    Here is my object on the MVC :
    Code (CSharp):
    1.  
    2. using System.ComponentModel.DataAnnotations;
    3. using System.Globalization;
    4.  
    5. namespace crcloudback.Models
    6. {
    7.     public class Score
    8.     {
    9.         public int scoreID { get; set; }
    10.         public int score { get; set; }
    11.         public string lvlName { get; set; }
    12.         public string playerName { get; set; }
    13.  
    14.     }
    15. }
    16.  
    In unity, I have this method to send the new score :
    Code (CSharp):
    1.  
    2. Leaderboard newScore = new Leaderboard();
    3.         newScore.LvlName = key;
    4.         newScore.Score = score;
    5.         newScore.Username = PlayerPrefs.GetString(ConstantsAndTypes.PlayerNameKey);
    6.         string scoreString = JsonConvert.SerializeObject(newScore);
    7.         byte[] body = Encoding.UTF8.GetBytes(scoreString);
    8.  
    9.  
    10.         var headers = new Dictionary<string,string>();
    11.         headers.Add("Content-Type", "application/json");
    12.        
    13.         WWW www = new WWW("http://localhost:50073/Scores/PostScore/", body, headers);
    14.        
    15.         yield return www;
    16.  
    17.         if (www.error != "")
    18.         {
    19.             Debug.Log(www.error);
    20.         }
    21.         else
    22.         {
    23.             Debug.Log(www.text);
    24.         }
    25.  
    And this is the object :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. //using Bitrave.Azure;
    4. using Newtonsoft.Json;
    5. //using System.Collections.Generic;
    6. //using Linq2Rest.Provider;
    7. using System;
    8.  
    9. public class Leaderboard
    10. {
    11.     [JsonProperty(PropertyName = "scoreID")]
    12.     public Int64 Id { get; set; }
    13.  
    14.     [JsonProperty(PropertyName = "playername")]
    15.     public string Username { get; set; }
    16.  
    17.     [JsonProperty(PropertyName = "score")]
    18.     public int Score { get; set; }
    19.  
    20.     [JsonProperty(PropertyName = "lvlname")]
    21.     public string LvlName { get; set; }
    22.  
    23.     public override string ToString()
    24.     {
    25.         return Score + " " + Username;
    26.     }
    27. }
    28.  
    Regarding the JSONs I have.

    This is the one I send to my MVC :
    {\"scoreID\":0,\"playername\":\"Fi\",\"score\":130,\"lvlname\":\"LvlBaseEasy\"}

    and this is the one I receive when I want my leaderboard :
    [{\"scoreID\":1,\"score\":10,\"lvlName\":\"Test\",\"playerName\":\"Test1\"},{\"scoreID\":2,\"score\":8,\"lvlName\":\"Test\",\"playerName\":\"Test1\"},{\"scoreID\":3,\"score\":5,\"lvlName\":\"Test\",\"playerName\":\"Test1\"}]

    Is somebody have an idea why my method PostScore receive a null score?

    Thank you,

    Karim