【发布时间】:2017-03-18 15:52:08
【问题描述】:
模型
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GetPeopleResult
{
public List<Person> people { get; set; }
public GetPeopleResult()
{
this.people = new List<People>();
}
public static GetPeopleResult CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<GetPeopleResult>(jsonString);
}
}
[System.Serializable]
public class Person
{
public long id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string displayImageUrl { get; set; }
public Person()
{
}
public static Person CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<Person>(jsonString);
}
}
JSON
{
"people":
[{
"id":1,"name":"John Smith",
"email":"jsmith@acme.com",
"displayImageUrl":"http://example.com/"
}]
}
代码
string json = GetPeopleJson(); //This works
GetPeopleResult result = JsonUtility.FromJson<GetPeopleResult>(json);
调用 FromJson 后,result 不为空,但 people 集合始终为空。
【问题讨论】: