2017-03-03 6 views
0

私のサーバーからJsonデータを取得し、そのデータをプロファイルに読み込もうとしています。 コードはuserProfile1 = JsonUtility.FromJson<UserProfile>(www.text);まで実行され、停止します。私は前後にデバッグ行を置こうとしたが、デバッグ後には決して起動しない。JsonUtility経由でJSONを読み取ることができません

私の問題はUnityのJsonUtilityがデータフォーマットを期待する方法と関係していると思いますが、何もエラーが戻ってこないことは確かではありません。

string baseurl = "http://55.55.55.55/api/"; 
public string loginId = "[email protected]"; 
public UserProfile userProfile1; 

void Start() 
{ 
    userProfile1 = new UserProfile();  
    StartCoroutine(GetUserProfile(loginId)); 
} 

IEnumerator GetUserProfile(string email) 
{ 
    string url = baseurl + "users/email/" + email; 

    // Call server 
    WWW www = new WWW(url); 

    yield return www; 

    // Read returned user profile 
    if (www.error == null) 
    { 
     userProfile1 = JsonUtility.FromJson<UserProfile>(www.text); 
    } 
    else 
    { 
     Debug.Log("WWW Error: " + www.error); 
    } 
} 

ここでは、プロファイルのためのクラスです:あなたのモデルには問題はありませんwww.text

[ 
    { 
     "_id":"58b92a058f9565e76d364437", 
     "first_name":"Test", 
     "last_name":"Name", 
     "email":"[email protected]", 
     "nick":"Tinkle", 
     "age":42, 
     "sex":"male", 
     "__v":0, 
     "inventory_slot":200000, 
     "join_date":"2017-02-26T00:36:10.266Z" 
    } 
] 
+1

これは、サーバーによって送信されたjson **配列**です。 'string myJson = www.error;を実​​行してから、' myJson = fixJson(myJson); 'を実行して' JsonHelper'で動作するようにjsonを修正します。今、 'UserProfile [] userProfile1 = JsonHelper.FromJson (myJson);' – Programmer

+1

を実行できます。 'fixJson'関数と' JsonHelper'クラスは、重複した質問で提供される答えに見つけることができます。 – Programmer

答えて

0

でつかんだよう

[System.Serializable] 
public class UserProfile 
{ 
    public string _id; 
    public string first_name; 
    public string last_name; 
    public string email; 
    public string nick; 
    public string join_date; 
    public int age; 
    public string sex; 
    public int inventory_slot; 
    public int __v; 
} 

はここで、サーバからのJSONデータです。私はwww.textが値を持っているとは思わない。データが到着していないので、おそらくまだnullです。

IEnumerator GetUserProfile(string email, Action<string> complete) 
    { 
     string url = baseurl + "users/email/" + email; 
     // Call server 
     WWW www = new WWW(url); 
     yield return www; 
     complete(www.text); 
    } 

そして、あなたはこれにあなたのStartCoroutineを変更する必要があります:私は何をやるべきことはこれです示唆

StartCoroutine(GetUserProfile(loginId, (data) => 
        { 
       var userProfile1 = JsonUtility.FromJson<UserProfile>(data); 
    })); 

はそれが役に立てば幸い!

P.S.あなたは何を知っています、私は問題があなたがリストを渡していると思っています。 JsonUtility.FromJson<UserProfile>(data)JsonUtility.FromJson<List< UserProfile>>(data)に変更してみてください。

関連する問題