Php To Unity Json / Array返回null

时间:2016-06-05 13:46:18

标签: c# php arrays json unity3d

我不明白为什么我的Array返回Null

在第一个示例上它起作用,但是当我尝试从WWW.text调用它时,它'返回null。

工作示例:

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);
    }
}
}

但这个没有。

  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)正在返回:

{"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"}]}

Debug.Log(_usersList.users [0] ._ users_username); return null

我想我可以用列表来做,但困扰我的是不理解为什么它不适用于数组。

谢谢!

编辑:

调用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;
    }
  }
  }

1 个答案:

答案 0 :(得分:2)

  

Debug.Log(w.text)正在返回:

{"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"}]}
  

Debug.Log(_usersList.users [0] ._ users_username); return null

UsersClass中的变量名称命名错误。它们与服务器发送给您的内容不匹配。请从_类中的每个变量名称的开头删除UsersClass

例如,您将用户名 _users_username命名为users_username而不是{ get; set; }。所有变量都受到影响,而不仅仅是一个。

要确保拼写正确,请转到here并粘贴从服务器收到的Json文件。它将为您提供完整的类和变量名称。复制这些变量并将其替换为您现在拥有的变量。只需确保从每个变量中删除_classname即可。 Unity Json无法解决这个问题。

同时停止命名您的班级pod 'DZNEmptyDataSet'。第一个字母应该大写,而不是下划线。