Search Unity

How to parse these json objects....

Discussion in 'Scripting' started by ludwig, Oct 27, 2020.

  1. ludwig

    ludwig

    Joined:
    Oct 15, 2013
    Posts:
    3
    Been trying to solve this for a while now. Hoping someone can help!

    From an API, this JSON data (can be multiple objects) is returned in this format:
    Code (JavaScript):
    1. {"0":{"character_name":"Ludwig_VanCover2","cid":"6","level":"84","xp":"100000","area":"baseGame_0000_mainStartingArea","x":"232.863","y":"44.8532","z":"198.191","rotation":"197.691","charClass":"Developer","guild":"<The Eternal Order>","active_title":"Developer","last_login":"2020-10-26 16:54:18"},"1":{"character_name":"Ludwig_VanCover2","cid":"7","level":"84","xp":"100000","area":"baseGame_0000_mainStartingArea","x":"232.863","y":"44.8532","z":"198.191","rotation":"197.691","charClass":"Developer","guild":"<The Eternal Order>","active_title":"Developer","last_login":"2020-10-26 16:54:18"}}
    My JSON CS helper file is:

    AreaPlayerJSON.cs
    Code (CSharp):
    1.  
    2. public class AreaPlayersJSON
    3. {
    4.         public string character_name { get; set; }
    5.         public string cid { get; set; }
    6.         public string level { get; set; }
    7.         public string xp { get; set; }
    8.         public string area { get; set; }
    9.         public string x { get; set; }
    10.         public string y { get; set; }
    11.         public string z { get; set; }
    12.         public string rotation { get; set; }
    13.         public string charClass { get; set; }
    14.         public string guild { get; set; }
    15.         public string active_title { get; set; }
    16.         public string last_login { get; set; }
    17.     }
    18.  
    19.     public class Root    {
    20.         public List<AreaPlayersJSON> AreaPlayersJSON { get; set; }
    21.     }
    In my project, the specific area I am having problems at is:

    Code (CSharp):
    1.  IEnumerator renderNearbyAreaChars(){
    2.         var userSession =  StaticClass.userInternalSession;
    3.         GameObject MMODATABASE = GameObject.Find("MMORPG");
    4.         DATABASE mmoDB = MMODATABASE.GetComponent<DATABASE>();
    5.             var characterLoadAPI = mmoDB.gameServer + "api/_game/localChar";
    6.             WWWForm form = new WWWForm();
    7.             form.AddField("sessionID", userSession);
    8.             form.AddField("x", mCharX.ToString());
    9.             form.AddField("y", mCharY.ToString());
    10.             form.AddField("area", mCharArea);
    11.  
    12.             UnityWebRequest www = UnityWebRequest.Post(characterLoadAPI, form);
    13.             yield return www.SendWebRequest();
    14.             if (www.isNetworkError) {
    15.                 Debug.Log(www.error);
    16.             } else {
    17.                 //var areaDataChars = JsonConvert.DeserializeObject<List<AreaPlayersJSON>>(www.downloadHandler.text);
    18.                 AreaPlayersJSON areaDataC = JsonConvert.DeserializeObject<AreaPlayersJSON>(www.downloadHandler.text);
    19.                 Debug.Log(www.downloadHandler.text);
    20.                 Debug.Log(areaDataC.character_name);
    21.              
    22.             }
    23.     }

    areaDataC.character_name is returning null. This is my first time working with JSON in C#. This is just a little personal project I am doing for fun. I have tried using the same code with <List> removed.

    Thank you for a nudge in the right direction!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Back up a bit and drop your JSON onto one of the free websites that give you a C# template to check that your template (the POCO you list above) is good. Notably, I usually use fields rather than properties as I find they are better-supported.

    If your POCO good, then reduce the problem set, make a SUPER simple JSON POCO object that has like one field with a string in it, create some JSON (again, online websites can help here), and see if you can get that to deserialize.

    Again, recommend fields over properties, at least until it works. Then you can experiment.