Search Unity

Resolved Parse Complex Json

Discussion in 'Scripting' started by danchavil, Sep 12, 2022.

  1. danchavil

    danchavil

    Joined:
    Jul 12, 2018
    Posts:
    32
    Hello everyone,
    i have been working with a coworker with json in dynamoDB. when i try to do a query, DynamoDB showme the next json

    Code (CSharp):
    1. [
    2.    {
    3.       "n_request":"",
    4.       "posicion":"",
    5.       "velocidad":"",
    6.       "client_id":"r2",
    7.       "status":"free",
    8.       "PartitionKey":3.0
    9.    },
    10.    {
    11.       "n_request":"",
    12.       "posicion":"",
    13.       "velocidad":"",
    14.       "client_id":"r1",
    15.       "status":"free",
    16.       "PartitionKey":2.0
    17.    },
    18.    {
    19.       "n_request":"",
    20.       "posicion":"",
    21.       "velocidad":"",
    22.       "client_id":"c2",
    23.       "status":"free",
    24.       "PartitionKey":1.0
    25.    },
    26.    {
    27.       "n_request":"",
    28.       "posicion":"",
    29.       "velocidad":"",
    30.       "client_id":"c1",
    31.       "status":"free",
    32.       "PartitionKey":0.0
    33.    }
    34. ]
    i do the deserialisation with one simple json and works. but when i try to deserialize this complex json i can't. here are objects and C# code.

    Code (CSharp):
    1. public class JsonObjects
    2. {
    3.     public DockiData[] allDockiData;
    4. }
    5.  
    6. [Serializable]
    7. public class DockiData
    8. {
    9.     public string n_request;
    10.     public string posicion;
    11.     public float velocidad;
    12.     public string client_id;
    13.     public string status;
    14.     public string PartitionKey;
    15.     public int posX, posY;
    16.     char[] deleteLetters = { '[', ']', ',' };
    17.  
    18.     public void SplitPos()
    19.     {
    20.         string[] positions = posicion.Split(deleteLetters, 4, StringSplitOptions.None);
    21.         posX = int.Parse(positions[1]);
    22.         posY = int.Parse(positions[2]);
    23.     }
    24. }
    and in here i deserialize the json
    Code (CSharp):
    1.     IEnumerator RequestJson()
    2.     {
    3.         UnityWebRequest carInformation = UnityWebRequest.Get(url);
    4.  
    5.  
    6.         yield return carInformation.SendWebRequest();
    7.  
    8.         if (carInformation.isHttpError || carInformation.isNetworkError)
    9.         {
    10.             Debug.Log(carInformation.error);
    11.  
    12.         }
    13.         else
    14.         {
    15.             string json = carInformation.downloadHandler.text;
    16.             print(json);
    17.             clients = JsonUtility.FromJson<JsonObjects>(json);
    18.  
    19.  
    20.        
    21.  
    22.          
    23.         }
    24.  
    25.  
    26.  
    27.     }
    when i run the project apear the next issue:

    ArgumentException: JSON must represent an object type.
    UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at <0618d0873e03447096799ae90b4ad4f1>:0)
    UnityEngine.JsonUtility.FromJson[T] (System.String json) (at <0618d0873e03447096799ae90b4ad4f1>:0)
    JsonLoader+<RequestJson>d__5.MoveNext () (at Assets/Scripts/JsonScripts/JsonLoader.cs:46)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <c62cc0ef748e4107b21e2999fa50d73a>:0)

    thanks for your help and regards.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,910
    your json is a naked array, which JsonUtility doesn't support. You must wrap it in a json object, as such:

    Code (JavaScript):
    1. {
    2.   "allDockiData": [
    3.     // your json array here
    4.   ]
    5. }
     
    Bunny83 likes this.
  3. danchavil

    danchavil

    Joined:
    Jul 12, 2018
    Posts:
    32
    thanks it works.