动态链接库
API
UnityWebRequest.Get(string uri)
{
"people": [
{
"name": "John",
"age": 30,
"city": "New York"
},
{
"name": "Alice",
"age": 25,
"city": "Los Angeles"
},
{
"name": "Bob",
"age": 35,
"city": "Chicago"
}
]
}
public class Person
{
/// <summary>
///
/// </summary>
public string name { get; set; }
/// <summary>
///
/// </summary>
public int age { get; set; }
/// <summary>
///
/// </summary>
public string city { get; set; }
}
public class People
{
/// <summary>
///
/// </summary>
public List <Person> people { get; set; }
}
IEnumerator Load(string url)
{
using(UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
yield return webRequest.SendWebRequest();
while (!webRequest.isDone)
{
yield return null;
}
if (webRequest.result == UnityWebRequest.Result.ConnectionError /*webRequest.isNetworkError*/ ||
webRequest.result == UnityWebRequest.Result.ProtocolError/*webRequest.isHttpError*/)
{
Debug.Log(webRequest.error.ToString());
yield return webRequest.error;
}
string jsonString = webRequest.downloadHandler.text;
// 解析Json文件
JsonData jsonData = JsonMapper.ToObject(jsonString);
// 获取数组
JsonData jsonData1 = jsonData["people"];
// 遍历数组中的对象
for(int i = 0; i < jsonData1.Count; i++)
{
// 获取数组中的对象
JsonData jsonData2 = jsonData1[i];
// 获取对象中的属性值
string name = (string)jsonData2 ["name"];
int age = (int)jsonData2 ["age"];
string city = (string)jsonData2 ["city"];
// 打印属性值
Debug.Log("Name: " + name + ", Age: " + age + ", City: " + city);
}
}
yield break;
}