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

[Solved] Json Utility Null

Discussion in 'Scripting' started by beanie4now, Mar 28, 2019.

  1. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Ok so I'm attempting to get client profiles from infusionsoft.

    I've googled everything possible it seems and still cant seem to get this working.
    This gets the data from the server fine.

    Code (CSharp):
    1. IEnumerator LoginTosqlDB()
    2.     {
    3.         WWWForm form = new WWWForm();
    4.         UnityWebRequest www = UnityWebRequest.Get("https://api.infusionsoft.com/crm/rest/v1/contacts?limit=1&access_token=mytokenbronoturs");
    5.         yield return www.SendWebRequest();
    6.         if (www.isNetworkError || www.isHttpError)
    7.         {
    8.             Debug.Log(www.error);
    9.         }
    10.         else
    11.         {
    12.  
    13.             Debug.Log("POST successful!");
    14.            
    15.             string json = www.downloadHandler.text;
    16.             Debug.Log(json);
    17.  
    18.             User u = new User();
    19.             u = JsonUtility.FromJson<User>(json);
    20.  
    21.             print(u.email);
    22.             //all the rest were printed below here name, address, etc
    23.         }
    24.  
    25.     }
    But when I check if any of the User values were propagated its all null.

    Code (CSharp):
    1. [System.Serializable]
    2. public class User
    3. {
    4.  
    5.     public string email;
    6.     //all the rest of the values were here name phone etc
    7.  
    8. }
    Json1.PNG
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    It looks like you are trying to deserialize it into a class it wasn't serialised from. You either need to convert the received string into your class or restructure your class to match the JSON.
     
  3. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Yeah, was trying to match my class to the Json. First try doing anything like this not sure where to go from here.

    Is there an easy-ish way to convert the received string into my class?
     
  4. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Ive been messing with
    Code (CSharp):
    1. json = json.Replace("[", "");
    2.             json = json.Replace("]", "");
    3.             json = json.Replace("{", "");
    4.             json = json.Replace("}", "");
    5.             json = json.Replace('"', ' ');
    and split() but it seems messy
     
  5. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    No need to replace anything. It looks like it starts with an array of contacts which has an array of something which contains a string and an enum, then a bool, then maybe a string array and then a date and so on.

    You could just use something like simplejson to get the nodes and convert them into what you want to use.
     
  6. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Yeah i guess idk how Json is support to work with all that nested array stuff like obviously i cant do
    Code (CSharp):
    1. public string[string email_addresses; bool email_opted_in; string[] addresses] Contacts;
    i was trying to purge off that contact array
     
  7. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    No, that's not even valid. You would need some classes. It's impossible to say without the JSON string to look at.
     
  8. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    {
    "contacts":[
    {"email_addresses":[{"email":"-----@yahoo.com","field":"EMAIL1"}],
    "email_opted_in":true,
    "addresses":[],
    "last_updated":"2019-03-28T02:06:46.000+0000",
    "tag_ids":[],
    "owner_id":null,
    "date_created":"2019-03-28T02:06:46.000+0000",
    "middle_name":null,
    "given_name":"Mmmmmm",
    "email_status":"Sssss",
    "phone_numbers":[{"number":"(---) 000-0000","extension":null,"field":"PHONE1","type":null}],
    "company":null,
    "id":14636,
    "family_name":"Afgerggg"}],
    "count":6236,
    "next":"--------------------------",
    "previous":"-------------------------------------"

    }
     
  9. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    I would totally implement the simple Json but i had just downloaded Json.net and it totally broke unity and i had to purge its files b4 unity would work again
     
  10. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Unity alrady has built in JSON support. There isn't really any need to install anything else other than simplejson if you wish to go deeper with it. Anyway, this should do the job for you;

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class TestJson : MonoBehaviour
    5. {
    6.     private string json = "{\"contacts\":[" +
    7.         "{\"email_addresses\":[" +
    8.         "{\"email\":\"-----@yahoo.com\"," +
    9.         "\"field\":\"EMAIL1\"}" +
    10.         "]," +
    11.         "\"email_opted_in\":true," +
    12.         "\"addresses\":[]," +
    13.         "\"last_updated\":" +
    14.         "\"2019-03-28T02:06:46.000+0000\"," +
    15.         "\"tag_ids\":[]," +
    16.         "\"owner_id\":null," +
    17.         "\"date_created\":" +
    18.         "\"2019-03-28T02:06:46.000+0000\"," +
    19.         "\"middle_name\":null," +
    20.         "\"given_name\":\"Mmmmmm\"," +
    21.         "\"email_status\":\"Sssss\"," +
    22.         "\"phone_numbers\":[" +
    23.         "{\"number\":\"(---) 000-0000\"," +
    24.         "\"extension\":null," +
    25.         "\"field\":\"PHONE1\"," +
    26.         "\"type\":null}" +
    27.         "]," +
    28.         "\"company\":null," +
    29.         "\"id\":14636," +
    30.         "\"family_name\":\"Afgerggg\"}" +
    31.         "]," +
    32.         "\"count\":6236," +
    33.         "\"next\":\"--------------------------\"," +
    34.         "\"previous\":\"-------------------------------------\"" +
    35.         "}";
    36.  
    37.     public Contacts contacts;
    38.  
    39.     public void Start()
    40.     {
    41.         contacts = JsonUtility.FromJson<Contacts>(json);
    42.     }
    43. }
    44.  
    45. [Serializable]
    46. public class Contacts
    47. {
    48.     public Contact[] contacts;
    49.     public int count;
    50.     public string next;
    51.     public string previous;
    52. }
    53.  
    54. [Serializable]
    55. public class Contact
    56. {
    57.     public Email[] email_addresses;
    58.     public bool email_opted_in;
    59.     public string[] addresses;
    60.     public string last_updated;
    61.     public string[] tag_ids;
    62.     public string owner_id;
    63.     public string date_created;
    64.     public string middle_name;
    65.     public string given_name;
    66.     public string email_status;
    67.     public PhoneDetails[] phone_numbers;
    68.     public string company;
    69.     public int id;
    70.     public string family_name;
    71. }
    72.  
    73. [Serializable]
    74. public class PhoneDetails
    75. {
    76.     public string number;
    77.     public string extension;
    78.     public string field;
    79.     public string type;
    80. }
    81.  
    82. [Serializable]
    83. public class Email
    84. {
    85.     public string email;
    86.     public string field;
    87. }
     
    beanie4now likes this.
  11. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    To use JsonUtility.FromJson, you need to have a class whose field names correspond to the Json properties you want to deserialize (it's OK to leave out any fields you don't care about; the deserializer will just skip them). For nested arrays or objects, you need a corresponding nested structure in your class.

    For instance, to get the email addresses in your example, you'd probably use something like:

    Code (CSharp):
    1. class EmailEntry {
    2.     public string email;
    3.     public string field;
    4. }
    5.  
    6. class Contact {
    7.     public List<EmailEntry> email_addresses;
    8.     public bool email_opted_in;
    9.     // etc.
    10. }
    11.  
    12. class ContactList {
    13.     public List<Contact> contacts;
    14. }
    15.  
    16.  
    17. // then call:
    18. JsonUtility.FromJson<ContactList>(json);
     
    Cyber-Dog likes this.
  12. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Just to bounce of that, not sure if its required with the Json Util your using but stypically you need to mark those classes as [System.Serialzable] to avoid null objects.
     
    mrtembo95 likes this.
  13. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Last edited: Mar 29, 2019
  14. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Works for me, there must be something different in your feed.

    Unity_2019-03-29_13-56-41.png
     
  15. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Got it working... no idea how but thanks for the help. Learned alot.
     
  16. mrtembo95

    mrtembo95

    Joined:
    Jul 1, 2021
    Posts:
    1
    Thank you so much, I was missing System.Serializable, I was using SerializeField instead.
     
    mmcveigh33 likes this.
  17. BlackGOD12

    BlackGOD12

    Joined:
    Apr 24, 2020
    Posts:
    2
    can you tell me how did you solved that?
    I have added System.serializable
     
  18. BlackGOD12

    BlackGOD12

    Joined:
    Apr 24, 2020
    Posts:
    2
    Other than system.serializable any changed=s to be made?
     
  19. Kafar

    Kafar

    Joined:
    Nov 29, 2012
    Posts:
    220
    Hello,

    I know this thread is old, I followed any step described above but I rest with the problem.

    this is my Json structure, I tried to deserialize with sonUtility.FromJson and JsonConvert.DeserializeObject but they returns null.

    Code (CSharp):
    1.  
    2. {
    3.     "ListTrack":[
    4.        {"Track":"No woman no cry"},
    5.        {"Track":"Is this love"},
    6.        {"Track":"No woman no cry"},
    7.        {"Track":"Is this love"},
    8.        {"Track":"Could you be loved"},
    9.        {"Track":"Buffalo soldier"},
    10.        {"Track":"Stir it up"},
    11.        {"Track":"Root, Rock, Reggae"},
    12.        {"Track":"Get up, stand up"},
    13.        {"Track":"One love"}]
    14. }
    and this is my code:

    Code (CSharp):
    1.  
    2. audiolistTrack = JsonConvert.DeserializeObject<ListTracks>(handler.text);
    3.                 //audiolistTrack = JsonUtility.FromJson<ListTracks>(handler.text);
    4.  
    5.  
    6. [Serializable]
    7. public class ListTrack
    8. {
    9.     public string Track;
    10. }
    11.  
    12. [Serializable]
    13. public class ListTracks
    14. {
    15.     public List<ListTrack> Tracks;
    16. }
    17.  
    Can someone help me on this? Many thanks.
     
  20. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Your JSON has the property named “ListTrack” but your property in the C# class is called Tracks.

    Either name them the same, or use attribute headers on the C# property to target a different name.
     
  21. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    The attribute header may only work for NewtonSoft. Unsure about unitys serializer
     
  22. Kafar

    Kafar

    Joined:
    Nov 29, 2012
    Posts:
    220
    Oh my God... thank you, I solved.
     
    Cyber-Dog likes this.