Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Parsing from Json Null

Discussion in 'Scripting' started by wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk, Feb 15, 2021.

  1. wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk

    wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk

    Joined:
    Jan 24, 2020
    Posts:
    9
    Hey guys, i probably don't know what i'm doing. Here's the thing
    I was using PHP and Sql, fetching some infos from the base, returning a Json file, it worked perfectly.
    However, i changed to Node.js and MongoDB, i just can't parse the return Json file.
    Probably i've tried everything out there, JsonUtility, SimpleJson, JsonConverter, you name it i probably tried it,
    always returns null ..

    Here's the code
    Code (CSharp):
    1.  
    2.  
    3.  
    4.     [Serializable]
    5.     public class schools
    6.     {
    7.  
    8.         [BsonId]
    9.         [BsonRepresentation(BsonType.ObjectId)]
    10.         public string _id;
    11.         public string name;
    12.         public string phone;
    13.         public string address;
    14.         public string hire_time;
    15.         public string district;
    16.         public string city;
    17.         public string province;
    18.         public string logo_url;
    19.  
    20.  
    21.     }
    22.  
    23.     IEnumerator RequestSearchSchool()
    24.     {
    25.         UnityWebRequest www = UnityWebRequest.Get(urlLink);
    26.         yield return www.SendWebRequest();
    27.  
    28.         if (www.isDone)
    29.         {
    30.             string res = www.downloadHandler.text;
    31.        
    32.             print(res);
    33.        
    34.             schools school = JsonUtility.FromJson<schools>(res);
    35.             print(school._id);
    36.             //   DrawUI();
    37.         }
    38.  
    39.     }
    40.  
    the "res" string prints the full Json file no problem.
    I'm not sure if the type of _id is causing the problem or not, but MongoDB uses ObjectId as a type for its _ids,
    i implemented its driver to use ObjectID type, but i tried all other types, all the same,
    Here's the Json
    Code (CSharp):
    1.  
    2. {
    3.     "schools": [
    4.         {
    5.             "_id": "60282477446130001cd994d4",
    6.             "name": "Name example",
    7.             "phone": "00099988876",
    8.             "address": "some address",
    9.             "hire_time": "1992-10-12",
    10.             "district": "Wuhuaqu",
    11.             "city": "Kunming",
    12.             "province": "Yunnan",
    13.             "logo_url": ""
    14.         }
    15.  
    16.     ]
    17.  
    18. }
    Any Ideas ?
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    Your type layout doesn't match the json, you need to structure your types to exactly match the data.
    You're missing the root object that contains the field "schools", which is an array of objects:

    Code (CSharp):
    1. [Serializable]
    2. public class Response
    3. {
    4.     public School[] schools;
    5. }
    6.  
    7. [Serializable]
    8. public class School
    9. {
    10.     // ...
    11. }
     
  3. wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk

    wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk

    Joined:
    Jan 24, 2020
    Posts:
    9
    Actually, my bad, after all you were absolutely right, i was so frustrated i messed it up, after cleaning the script, it did work, great work man !