0

I'm pulling Json data from my server and intending to read it into a profile. The code runs right up to the userProfile1 = JsonUtility.FromJson<UserProfile>(www.text); and stops. I tried putting a debug line before and after and the after debug never fires.

I think the issue perhaps has something to do with how Unity's JsonUtility expects the data format to be, but I'm not sure exactly what as I get no error back.

string baseurl = "http://55.55.55.55/api/";
public string loginId = "test@spam.com";
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);
    }
}

Here's the class for the profile:

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

Here's the Json data from the server as grabbed by www.text

[
    {
        "_id":"58b92a058f9565e76d364437",
        "first_name":"Test",
        "last_name":"Name",
        "email":"tinkle@spam.com",
        "nick":"Tinkle",
        "age":42,
        "sex":"male",
        "__v":0,
        "inventory_slot":200000,
        "join_date":"2017-02-26T00:36:10.266Z"
    }
]
hendryanw
  • 1,819
  • 5
  • 24
  • 39
roskelld
  • 1,050
  • 1
  • 14
  • 25
  • 1
    This is a json **array** being sent by the server. Do `string myJson = www.error;` then fix the json to work with `JsonHelper` by doing: `myJson = fixJson(myJson);`. Now, you can do `UserProfile[] userProfile1 = JsonHelper.FromJson(myJson );` – Programmer Mar 03 '17 at 10:46
  • 1
    . You can find both the `fixJson` function and the `JsonHelper` class on the answer provided in the duplicated question. – Programmer Mar 03 '17 at 10:46

1 Answers1

0

There's no problem with your model. I don't think the www.text has a value at all. It's probably still null, because the data hasn't arrived. I suggest what you should do is this:

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

And you should change your StartCoroutine to this:

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

Hope it helps!

P.S. Or you know what, I'm starting to think that the problem is that you're passing a List. Try changing JsonUtility.FromJson<UserProfile>(data) to JsonUtility.FromJson<List< UserProfile>>(data).

mindOfAi
  • 4,412
  • 2
  • 16
  • 27