Kyan Kyan - 5 months ago 71
JSON Question

Php To Unity Json / Array Return null

I don't understand why my Array return Null

On the first Example it work but when I try to call it from a WWW.text it' return null.

Working Example :

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class _testScripts_JSON : MonoBehaviour
{

void Start() {

string text = "{\"items\": [{\"ID\":\"1\",\"char_name\":\"KeeLo\",\"level\":\"12\",\"agility\":\"11\"}]}";

MyClassList list = MyClassList.CreateFromJSON(text);

Debug.Log(list.items[0].agility);
}


[System.Serializable]
public class MyClassList
{
public MyClass[] items;

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

[System.Serializable]
public class MyClass
{
public int ID;
public string char_name;
public int level;
public int agility;

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


But this one doesn't.

public class _myScripts_loading_content : MonoBehaviour {

[System.Serializable]
public class UsersClassList
{
public UsersClass[] users;

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

[System.Serializable]
public class UsersClass
{
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_type_name;

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

WWWForm form = new WWWForm();
form.AddField("_usersUsername", _scripts_myScripts_connect_to_db._usersUsername_string);
form.AddField("_usersPassword", _scripts_myScripts_connect_to_db._usersPassword_string);
form.AddField("_classToCall", _functionCalled);

WWW w = new WWW("http://localhost/myhouse/_functionCalled.php", form);

yield return w;

Debug.Log(w.text);

UsersClassList _usersList = UsersClassList.CreateFromJSON(w.text);

Debug.Log(_usersList.users[0]._users_username);
}


Debug.Log(w.text) is returning this :

{"users": [{"users_id":"1","users_username":"Kabendji","users_password":"password","users_email":"Kyan.Kabendji@gmail.com","users_name":"Kabendji","users_surname":"Kyan","users_adress":"adress","users_zipCode":"zipCode","users_phone":"phone number","users_picture":null,"users_date_login":"2016-06-05 15:40:49","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 the Debug.Log(_usersList.users[0]._users_username); return null

I think I can do it with a list, but what bothers me is to not understanding why it does not work with an array.

Thanks you!

Edit :

Script that call the Loading

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class _myScripts_connect_to_db : MonoBehaviour {

public Text _messageWarning_text;
public InputField _users_username_inputField, _users_password_inputField;

public string _usersUsername_string, _usersPassword_string;

_myScripts_xml_languages _script_myScripts_xml_languages;
_myScripts_loading_content _script_myScripts_loading_content;

private string _functionCalled = "_login";

// Loading Screen

public GameObject _loadingScreen;

// Animation UI
private Animator _animator_loginPage;


// Use this for initialization

public void Start()
{

_script_myScripts_xml_languages = GetComponent<_myScripts_xml_languages>();
_script_myScripts_loading_content = GetComponent<_myScripts_loading_content>();

_animator_loginPage = GameObject.Find("_login_page").GetComponent<Animator>();

}

public void Connection()
{
_messageWarning_text.text = "";

_usersUsername_string = _users_username_inputField.text;
_usersPassword_string = _users_password_inputField.text;

if(string.IsNullOrEmpty(_usersUsername_string) || string.IsNullOrEmpty(_usersPassword_string) )
{
_messageWarning_text.text = _script_myScripts_xml_languages.Strings["fill_field"] as string;
}

else
{
WWWForm form = new WWWForm();
form.AddField("_usersUsername", _usersUsername_string);
form.AddField("_usersPassword", _usersPassword_string);
form.AddField("_current_languages", _script_myScripts_xml_languages._current_languages);
form.AddField("_classToCall", _functionCalled);
WWW w = new WWW("http://localhost/myhouse/_functionCalled.php", form);
StartCoroutine(Login(w));
}
}

private IEnumerator Login(WWW _connection_w)
{
yield return _connection_w;

if(_connection_w.error == null)
{
if (_connection_w.text == _script_myScripts_xml_languages.Strings["loginSuccess"] as string)
{
//You're Connected // Do Something.
_messageWarning_text.text = _connection_w.text;

_loadingScreen.SetActive(true);

yield return _script_myScripts_loading_content.StartCoroutine(_script_myScripts_loading_content.LoadingContent());

_loadingScreen.SetActive(false);

_animator_loginPage.SetBool("isHidden", true);
//Debug.Log(_connection_w.text);
}

else
{
_messageWarning_text.text = _script_myScripts_xml_languages.Strings["error"] as string + _connection_w.text;
}
}

else
{
_messageWarning_text.text = _script_myScripts_xml_languages.Strings["error"] as string + _connection_w.text;
}
}
}

Answer

Debug.Log(w.text) is returning this :

{"users": [{"users_id":"1","users_username":"Kabendji","users_password":"password","users_email":"Kyan.Kabendji@gmail.com","users_name":"Kabendji","users_surname":"Kyan","users_adress":"adress","users_zipCode":"zipCode","users_phone":"phone number","users_picture":null,"users_date_login":"2016-06-05 15:40:49","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 the Debug.Log(_usersList.users[0]._users_username); return null

The variable names in UsersClass are badly named. They do not match with what the server is sending you. Please remove _ from the beginning of each variable name in the UsersClass class.

For example, you named the username _users_username instead of users_username. All the variables are affected, not just one.

To make sure you got all the spellings right, go here and paste the Json file you received from the server. It will give you a complete class and variable names. Copy those variables and replace them with what you have now. Just make sure to remove { get; set; } from each variable. Unity Json can't work with that.

Also stop naming your classes _classname. The first letter should be capitalized and not an underscore.

Comments