JsonUtility Unexpected node type

Hello

I’try to pass a php array to c# Unity with JsonUtility.

But there’s something wrong.

Why this work :

 {"users": [ {"users_id":"1","users_username":"Kabendji","users_password":"password","users_email":"email@gmail.com","users_name":"Kabendji","users_surname":"Kyan","users_adress":"adress","users_zipCode":"zipCode","users_phone":"000000","users_picture":"Image_null","users_date_login":"2016-06-05 18:56:58","users_date_registration":"2016-06-01 18:29:29","customers_group_customers_group_name":"Admin Group Test","country_country_name":"Belgium","users_type_users_type_name":"Administrator"}]}

And this one not :

{"users":{"users_id":"1","users_username":"Kabendji","users_password":"password","users_email":"email@gmail.com","users_name":"Kabendji","users_surname":"Kyan","users_adress":"adress","users_zipCode":"zipCode","users_phone":"000000","users_picture":"Image_null","users_date_login":"2016-06-05 19:04:11","users_date_registration":"2016-06-01 18:29:29","customers_group_customers_group_name":"Admin Group Test","country_country_name":"Belgium","users_type_users_type_name":"Administrator"}}

With the first one, I can’t use PHP array to parse Json from PHP to C# Unity

So I have to do something like this to make it work :

echo '{"users": [ '.json_encode($_row).']}';

instead of using a simple way like :

$_array = array();
$_array['users'] = $_row;
echo json_encode($_array);

In case of multi query it’s way harder without using array.

So can someone explain me, what I did wrong ?

Thanks you :wink:

!Bump

Those are different structures - in the first “users” contains an array of objects, in the second it just contains one object. So, what does the class you’re deserializing it into look like?

To expound on this… your first has square brackets “[” and “]”. These signify an array. If you don’t have those, then it expects it to be a singular instance.

[   System.Serializable]
    public struct MyClassList
    {  
        [System.Serializable]
       public structUsers
        {
           public int users_id;
           public string users_username;
           public string users_password;
           public string users_email;
           public string users_name;
           public string users_surname;
           public string users_adress;
           public string users_zipCode;
           public string users_phone;
           public string users_picture;
           public string users_date_login;
           public string users_date_registration;
           public string customers_group_customers_group_name;
           public string country_country_name;
           public string users_type_users_type_name;
        }

        public Users[] users;

        [System.Serializable]
        public struct Country
        {
           public int country_id;
           public string country_name;
        }

       public Country[] country;

        public static MyClassList CreateFromJSON(string jsonString)
        {
               return JsonUtility.FromJson<MyClassList>(jsonString);
        }
    }

I understand that’s it’s a singular instance.

But is there no other way to get it work without adding :

'{"users": [ '.json_encode($_row).']}';

Because when there’s only on row it can be easy, but to return several row.
I have to do this :

            echo '{"country": [ ';
            while($_row = mysqli_fetch_array($_query, MYSQLI_ASSOC))
            {
                echo json_encode($_row).',';
            }
            echo ']}';
            die;

And then delete 3 last character and add ]}

I hope there’s a better way :wink:

Well, it’s an array, so it has to be sent as an array. In the “return several rows” example, what would you be able to do differently anyway, if it wasn’t?

If you dislike the “deleting the extra comma at the end” method, you could instead do:

echo '{"country": [ ';
$_first = true;
while($_row = mysqli_fetch_array($_query, MYSQLI_ASSOC)) {
   if(!$_first) {
      echo ',';
   }
   $_first = false;
   echo json_encode($_row);
}
echo ']}';
die;
1 Like

Thanks for the tips :wink:

I was looking for a proper way to do it, but if i have to parse like that , it’s ok for me :slight_smile:

I had almost forgotten how awful PHP was. :confused:

If you were me what will you do?
Something else than Mysql + PHP?

Nah, PHP and MySQL is fine if that’s what you know and your hosting provider supports. I personally prefer C# / WebAPI / MSSQL. MySQL doesn’t do a good job of (rather make it easy to) protecting against SQL injection attacks. For instance, how are you building your SQL Query?

That being said… you should look at using classes and JsonSerializable in PHP… it would make your job easier and you wouldn’t have to manually construct the JSON:

http://php.net/manual/en/jsonserializable.jsonserialize.php

1 Like