Unexpected character encountered while parsing value: . Path '', line 0, position 0. Reading JSON

Hi, Unity with JsonReader from JSON.NET throws me an exception.

JsonReaderException: Unexpected character encountered while parsing value: . Path ‘’, line 0, position 0.

The problem is, i checked multiple ways of parsing with classes, just dictionaries, and nothing.
i’m using UnityWebRequest to get a PHP to echo me the JSON of all values from a Table, and i don’t know what to do.

JSON that the PHP Gives me:
[{"deptoid":"101","piso":"1","disponible":"1"},{"deptoid":"102","piso":"1","disponible":"0"},{"deptoid":"201","piso":"2","disponible":"1"},{"deptoid":"301","piso":"3","disponible":"1"},{"deptoid":"401","piso":"4","disponible":"1"}]

Code:

public class Retrieval : MonoBehaviour
{
    //private string secretKey = "RitoLab$";
    public string DeptosURL =
             "https://ritolab.cl/demounitydepartamentos/getdptos.php";
    // Start is called before the first frame update
    public void GetDptoBtn()
    {
    }

    void Start()
    {
        StartCoroutine("GetDpto");
    }

    IEnumerator GetDpto()
    {
        UnityWebRequest hs_get = UnityWebRequest.Get(DeptosURL);
        yield return hs_get.SendWebRequest();
if (hs_get.error != null)
        {
            Debug.Log("Hubo un error Bajando informacion de los departamentos: " + hs_get.error);
        }
        else
        {
            string dataText = System.Text.Encoding.UTF8.GetString(hs_get.downloadHandler.data);
            Debug.Log(dataText);
            Rootobject departamentos = JsonConvert.DeserializeObject<Rootobject>(dataText);
foreach (Class1 depto in departamentos.Property1)
            {
                Debug.Log("Departamento 1: Piso: " + depto.piso + " DepartamentoID: " + depto.deptoid + " y Disponibilidad: " + depto.disponible);
            }

[System.Serializable]
    public class Rootobject
    {
        public Class1[] Property1 { get; set; }
    }
    [System.Serializable]
    public class Class1
    {
        public string deptoid { get; set; }
        public string piso { get; set; }
        public string disponible { get; set; }
    }

I have read multiple threads and none gives me a solution to my problem, so i thought i would write this for Help.

Thanks in advance.

Unity’s JSON parser cannot read “naked arrays” like you have. The way to solve this is to wrap the json array in a json object:

{
  "Property1":
    [{"deptoid":"101","piso":"1","disponible":"1"},{"deptoid":"102","piso":"1","disponible":"0"},    {"deptoid":"201","piso":"2","disponible":"1"},{"deptoid":"301","piso":"3","disponible":"1"},    {"deptoid":"401","piso":"4","disponible":"1"}]
}

You can do this right in your code:

dataText = "{\"Property1\": " + dataText + "}";

EDIT: I misread your post a bit,. i see you’re using Newtonsoft. Problem is your JSON doesn’t match your object structure. The code I posted here will change the JSON structure to match your class structure.

Adding onto what Praetor says above, beware of this:

Problems with Unity “tiny lite” built-in JSON:

In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window → Package Manager).

https://forum.unity.com/threads/jso…-not-working-as-expected.722783/#post-4824743

Also, always be sure to leverage sites like:

https://csharp2json.io

PS: for folks howling about how NewtonSoft JSON .NET will “add too much size” to your game, JSON .NET is like 307k in size, and it has the important advantage that it actually works the way you expect a JSON serializer to work in the year 2021.

I actually used in the code the Newtonsoft JSON.NET, in the actual script i have newtonsoft namespace inserted, i must’ve not copied it though.
Thanks for the answers, I finally Resolved it by making a “fake JSON” i have the PHP give me the values separated by “_” and each row separated by “-” , so i used a class and split to get them and set them on the class, now it works flawlessly.
PHP to Get new format of values:

$sql = 'SELECT * FROM Depas';
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $row)
{
echo $row['piso']. "_";
echo $row['deptoid']. "_";
echo $row['disponible']. "-";
}

And i get this:
1_101_1-1_102_0-2_201_1-3_301_1-4_401_1-

so i do this to split them, delete the last one since it gives an extra “-”

C#:

public class Retrieval : MonoBehaviour
{
public string DeptosURL =
             "https://ritolab.cl/demounitydepartamentos/getdptos.php";
    public String[] textSplit;
    public List<Departamento> Departamentos = new List<Departamento>();

    void Start()
    {
        StartCoroutine("GetDpto");
    }

    IEnumerator GetDpto()
    {
        UnityWebRequest hs_get = UnityWebRequest.Get(DeptosURL);
        yield return hs_get.SendWebRequest();
        if (hs_get.error != null)
        {
            Debug.Log("Hubo un error Bajando informacion de los departamentos: " + hs_get.error);
        }
        else
        {
            string dataText = System.Text.Encoding.UTF8.GetString(hs_get.downloadHandler.data);
            Debug.Log(dataText); // For Debugging Raw Text
            textSplit = dataText.Split('-'); // To make an array with all rows encapsulated
            Array.Resize(ref textSplit, textSplit.Length - 1); //to delete the last one, since it's empty due to formatting in php.
            for(int i = 0; i < textSplit.Length-1; i++)
            {
                String[] textSplit2 = textSplit[i].Split('_'); //to split each one into an array of single values, then i assign them to a new class inside a list.
                Departamento depa = new Departamento();
                depa.piso = textSplit2[0];
                depa.deptoid = textSplit2[1];
                depa.disponible = textSplit2[2];
                Departamentos.Add(depa);
            }
            Debug.Log(Departamentos[0].deptoid);
            Debug.Log(Departamentos[1].deptoid);
            Debug.Log(Departamentos[2].deptoid);
        }
    }

public class Departamento
    {
        public string deptoid { get; set; }
        public string piso { get; set; }
        public string disponible { get; set; }
    }