如何在Unity中获得JSON数据,其中有许多变量?



我做了这个代码,当API文本有一个变量,它的值,但当文本是这样的我的脚本不工作,如何修复它?我试图只得到一个变量,但它没有工作,我得到null。

API文本,我的代码工作:https://catfact.ninja/fact

API文本,我的代码不工作:https://randomuser.me/api

我编辑代码从许多变量JSON中获取数据,但再次得到null:

using UnityEngine;
using TMPro;
using UnityEngine.Networking;
using System.Collections;
using System;
using Newtonsoft.Json;
public class Service : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI _text;
private void Start()
{
StartCoroutine(GetRequest("https://randomuser.me/api"));
}
private IEnumerator GetRequest(String url)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
yield return webRequest.SendWebRequest();
switch (webRequest.result)
{
case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError:
_text.text = "Error: " + webRequest.error;
break;
case UnityWebRequest.Result.ProtocolError:
_text.text = "HTTP Error: " + webRequest.error;
break;
case UnityWebRequest.Result.Success:
if (CheckAPI(webRequest))
{
Name name = JsonConvert.DeserializeObject<Name>(webRequest.downloadHandler.text);
if (name.title == null || name.first == null || name.last == null)
{
_text.text = "Error: no information with such variables";
}
else
{
_text.text = name.title + " " + name.first + " " + name.last;
}
}
else
{
_text.text = "Error: there are no API data";
}
break;
}
}
}
private bool CheckAPI(UnityWebRequest data)
{
string contentType = data.GetResponseHeader("Content-Type");
if (!string.IsNullOrEmpty(contentType) && contentType.Contains("application/json"))
{
return true;
}
else
{
return false;
}
}
public class Name
{
public string title { get; set; }
public string first { get; set; }
public string last { get; set; }
}
public void NewRequest()
{
Start();
}
}

在第一个示例中,您使用的是类"Fact"使用(我想)这样的结构(或类似的结构):

public class Fact
{
public string fact { get; set; }
public int length { get; set; }
}

,用

将json反序列化为对象
Fact fact = JsonConvert.DeserializeObject<Fact>(webRequest.downloadHandler.text);

,但是在第二个例子中响应是不同的,所以你需要这些类:

public class myNewClass
{
public List<Result> results { get; set; }
public Info info { get; set; }
}
public class Coordinates
{
public string latitude { get; set; }
public string longitude { get; set; }
}
public class Dob
{
public DateTime date { get; set; }
public int age { get; set; }
}
public class Id
{
public string name { get; set; }
public string value { get; set; }
}
public class Info
{
public string seed { get; set; }
public int results { get; set; }
public int page { get; set; }
public string version { get; set; }
}
public class Location
{
public Street street { get; set; }
public string city { get; set; }
public string state { get; set; }
public string country { get; set; }
public int postcode { get; set; }
public Coordinates coordinates { get; set; }
public Timezone timezone { get; set; }
}
public class Login
{
public string uuid { get; set; }
public string username { get; set; }
public string password { get; set; }
public string salt { get; set; }
public string md5 { get; set; }
public string sha1 { get; set; }
public string sha256 { get; set; }
}
public class Name
{
public string title { get; set; }
public string first { get; set; }
public string last { get; set; }
}
public class Picture
{
public string large { get; set; }
public string medium { get; set; }
public string thumbnail { get; set; }
}
public class Registered
{
public DateTime date { get; set; }
public int age { get; set; }
}
public class Result
{
public string gender { get; set; }
public Name name { get; set; }
public Location location { get; set; }
public string email { get; set; }
public Login login { get; set; }
public Dob dob { get; set; }
public Registered registered { get; set; }
public string phone { get; set; }
public string cell { get; set; }
public Id id { get; set; }
public Picture picture { get; set; }
public string nat { get; set; }
}

public class Street
{
public int number { get; set; }
public string name { get; set; }
}
public class Timezone
{
public string offset { get; set; }
public string description { get; set; }
}

并使用:

反序列化
myNewClass myDeserializedClass = JsonConvert.DeserializeObject<myNewClass>(webRequest.downloadHandler.text);

当然,把你的主类的名字改成你想要的任何名字。

您可以使用以下工具处理此情况或任何其他情况:

https://json2csharp.com/

如果你只需要一个名字你可以解析一个json字符串并反序列化name属性

Name name = JObject.Parse(webRequest.downloadHandler.text)["results"][0]["name"]
.ToObject<Name>();