json - Unity JSON Serializer 不允许调用 JSON 字段 n JSON 对象
问题描述
有一个问题,我无法从抓取的网站调用嵌套的 JSON 对象。抓取过程完美无缺,但 JSON 序列化部分是唯一的问题。我的代码如下所示:
private void GetHtmlAsync()
{
var url = "https://opentdb.com/api.php?amount=10";
var httpClient = new HttpClient();
var html = httpClient.GetStringAsync(url);
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(MyDetail));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(html.Result));
stream.Position = 0;
MyDetail dataContractDetail = (MyDetail) jsonSerializer.ReadObject(stream);
text.text = "" + dataContractDetail.results[1];
//text.text = string.Concat("Test: ", dataContractDetail.question, " " + dataContractDetail.correct_answer);
}
public class MyDetail
{
[DataMember]
public Dictionary<string, questions> results
{
get;
set;
}
public class questions
{
public string question { get; set; }
public string correct_answer { get; set; }
}
[DataMember]
public string response_code
{
get;
set;
}
}
此代码是不起作用的代码,因为我尝试通过执行“results [1]”来调用结果中的第一个对象,在我通过执行“results [ 1].问题”。这种语法似乎很合理,所以我不明白为什么它不起作用。我的 JSON 文件如下所示:
{
"response_code": 0,
"results": [
{
"category": "Entertainment: Video Games",
"type": "multiple",
"difficulty": "medium",
"question": "What is the name of the virus in "Metal Gear Solid 1"?",
"correct_answer": "FOXDIE",
"incorrect_answers": [
"FOXENGINE",
"FOXALIVE",
"FOXKILL"
]
},
{
"category": "Geography",
"type": "multiple",
"difficulty": "easy",
"question": "What is the official language of Costa Rica?",
"correct_answer": "Spanish",
"incorrect_answers": [
"English",
"Portuguese",
"Creole"
]
},
{
"category": "Entertainment: Video Games",
"type": "multiple",
"difficulty": "medium",
"question": "In Fallout 4, which type of power armor is first encountered in the early mission "When Freedom Calls" in a crashed Vertibird?",
"correct_answer": "T-45",
"incorrect_answers": [
"T-51",
"T-60",
"X-01"
]
},
{
"category": "Politics",
"type": "boolean",
"difficulty": "medium",
"question": "George W. Bush lost the popular vote in the 2004 United States presidential election.",
"correct_answer": "False",
"incorrect_answers": [
"True"
]
},
{
"category": "Entertainment: Video Games",
"type": "multiple",
"difficulty": "medium",
"question": "In "Halo 2", what is the name of the monitor of Installation 05?",
"correct_answer": "2401 Penitent Tangent",
"incorrect_answers": [
"343 Guilty Spark",
"031 Exuberant Witness",
"252 Biodis Expolsion"
]
},
{
"category": "Entertainment: Books",
"type": "multiple",
"difficulty": "medium",
"question": "The book "Fahrenheit 451" was written by whom?",
"correct_answer": "Ray Bradbury",
"incorrect_answers": [
"R. L. Stine",
"Wolfgang Amadeus Mozart",
"Stephen King"
]
},
{
"category": "Entertainment: Cartoon & Animations",
"type": "multiple",
"difficulty": "hard",
"question": "In "Rick and Morty", from which dimension do Rick and Morty originate from?",
"correct_answer": "C-137",
"incorrect_answers": [
"J1977",
"C-136",
"C500-a"
]
},
{
"category": "Entertainment: Video Games",
"type": "multiple",
"difficulty": "hard",
"question": "In which game did the character "Mario" make his first appearance?",
"correct_answer": "Donkey Kong",
"incorrect_answers": [
"Super Mario Bros.",
"Super Mario Land",
"Mario Bros."
]
},
{
"category": "Entertainment: Film",
"type": "multiple",
"difficulty": "hard",
"question": "What was Humphrey Bogart's middle name?",
"correct_answer": "DeForest",
"incorrect_answers": [
"DeWinter",
"Steven",
"Bryce"
]
},
{
"category": "Entertainment: Cartoon & Animations",
"type": "boolean",
"difficulty": "medium",
"question": "In "Avatar: The Last Airbender" and "The Legend of Korra", Lavabending is a specialized bending technique of Firebending.",
"correct_answer": "False",
"incorrect_answers": [
"True"
]
}
]
}
解决方案
您的代码中有很多问题。我不知道您正在使用的所有库,但我会这样做。
首先,您开始 aGetStringAsync
但您立即继续而不等待结果。我不知道您正在使用的所有库,也许它应该是这样的?
但是我宁愿使用 Unity 的UnityWebRequest.Get
private void GetHtmlAsync()
{
StartCoroutine(DownloadJson());
}
private IEnumerator DownloadJson()
{
var url = "https://opentdb.com/api.php?amount=10";
using(var uwr = UnityWebRequest.Get(url))
{
// send the request and wait for result
yield return uwr.SendWebRequest();
// Check for success!
if(uwr.isNetworkError || uwr.isHttpError || !string.IsNullOrWhiteSpace(uwr.error))
{
Debug.LogError($"Download failed with {uwr.responseCode} reason: {uwr.error}", this);
yield break;
}
var json = uwr.DownloadHandler.text;
// ... se below
}
}
同样,我不知道您的 JSON 库,但您的类似乎与 JSON 数据结构不匹配(通过简单地通过json2csharp干扰它)
[Serializable]
public class Result
{
public string category;
public string type;
public string difficulty;
public string question;
public string correct_answer;
public List<string> incorrect_answers;
}
[Serializable]
public class MyDetail
{
public int response_code;
public List<Result> results;
}
对于 Unity,我会使用[Serializable]
并删除所有的{get;set}
,以便不使用属性而是使用字段。
然后你可以简单地使用 Unity 的JsonUtility
...
MyDetail dataContractDetail = JsonUtility.FromJson<MyDetail>(json);
然后正如评论中提到的,注意 c# 中的数组索引是0
基于 - 的,所以第一个元素是
var firstResult = dataContractDetail.results[0];
现在的问题是你想在你的文字上看到什么?firstResult
不是,string
而是一个有各种成员的类!例如,您可能想显示如下问题
text.text = firstResult.question;
推荐阅读
- java - 由于属性文件位置类型,应用程序或集成测试 Spring 上下文创建失败
- .net - 当窗体没有焦点时激活 ContextMenuStrip
- actionscript-3 - 问题:网站和 Flash 加载我的 SWF 客户端的旧版本
- javascript - ajax 响应和 html 无法正常工作
- amazon-rds - AWS Neptune 读取器和写入器必须是相同的实例类型吗?
- visual-studio-code - 为什么 VS Code 终端在初始提示中添加 cr-lf 字符?
- pandas - 在循环熊猫中添加列
- git - git:你怎么能在 git-status 中使用 `R`、`C` 和 `U` 标志
- node.js - 我怎么打电话
Firebase 云函数中的 .listCollections()? - php - antergos 从 htaccess 切换到 php 7.0