Search Unity

How to convert a list into Json?Code not working

Discussion in 'Scripting' started by zyonneo, Aug 15, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    I was trying to convert a list into json format.The jsonstring I am getting is always empty.Code below

    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class ListContainer
    4. {
    5.  
    6.     public List<PlayerHandler> SaveValues;
    7.  
    8.     public ListContainer(List<PlayerHandler> _saveVal)
    9.     {
    10.         SaveValues = _saveVal;
    11.     }
    12.  
    13. }
    14.  
    15. public class Testing : MonoBehaviour
    16. {
    17. List<PlayerHandler> getAlldata;
    18.  
    19. var container = new ListContainer(getAlldata);
    20. string jsonstring = JsonUtility.ToJson(container);
    21. //the jsonstring  is always empty
    22.  
    23. }
     
    Last edited: Aug 15, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    This question asked million times before. Unity serializer do not support arrays as root object as stated in the manual.

    Code (CSharp):
    1. [Serializable]
    2. class Container<T> {
    3. public T[] array;
    4. }
     
  3. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    From the below code I once got the json string.So what is the difference I am passing the same list in the below code.

    Code (CSharp):
    1.  
    2. public struct ListContainer

    3. {
    4.   public List<DataHandlers> SaveValues;


    5.    /// <summary>

    6.   /// Constructor

    7.   /// </summary>

    8.   /// <param name="_dataList">Data list value</param>

    9.   public ListContainer(List<DataHandlers> _dataList)
    10.     {
  
    11.     SaveValues = _dataList;
    12.     }
    13. }
    14.  
    15.  
    16.  SaveValues.Clear();
          
    17.  SaveValues = UnityARHitTestExample.HittestInstance.Modelinfo;


    18.  ListContainer container = new ListContainer(SaveValues);

            
    19.  string jsonstring = JsonUtility.ToJson(container);
            
    20.  Debug.Log("Save JSON String = " + jsonstring);

    Where Modelinfo
    Code (CSharp):
    1. public List<DataHandlers> Modelinfo = new List<DataHandlers>();
    2.  
    3.  
    4. [Serializable]
    5. public class DataHandlers
    6. {
    7.     public int modelid;
    8.     public Vector3 localposinmap;
    9.     public Quaternion localrotinmap;
    10.     public Vector3 sizeofobj;
    11.  
    12.  
    13.     public DataHandlers(int idmodel,Vector3 localpos, Quaternion localrot, Vector3 objsize)
    14.     {
    15.         this.modelid = idmodel;
    16.         this.localposinmap = localpos;
    17.         this.localrotinmap = localrot;
    18.         this.sizeofobj = objsize;
    19.  
    20.  
    21.     }
    22.  
    23.  
    24. }
     
    Last edited: Aug 15, 2019
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Your first example getAllData is null, I'm not sure what jsonUtility does with that to begin with. Beyond that, I usually use json.net for my json stuff.
     
  5. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    In both the cases I am using a list.In one I got a json string and in another I dont get it.