Unity JSON序列化程序不允许在JSON对象中调用JSON字段

Unity JSON序列化程序不允许在JSON对象中调用JSON字段,json,unity3d,jsonserializer,Json,Unity3d,Jsonserializer,问题是,我无法从一个被刮掉的网站调用嵌套的JSON对象。刮取过程工作得很好,但JSON序列化部分是唯一的问题。我的代码如下所示: private void GetHtmlAsync() { var url = "https://opentdb.com/api.php?amount=10"; var httpClient = new HttpClient(); var html = httpClient.GetStringAsync(url

问题是,我无法从一个被刮掉的网站调用嵌套的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;
        }
    }
{
"response_code": 0,
"results": [
{
"category": "Entertainment: Video Games",
"type": "multiple",
"difficulty": "medium",
"question": "What is the name of the virus in &quot;Metal Gear Solid 1&quot;?",
"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 &quot;When Freedom Calls&quot; 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 &quot;Halo 2&quot;, 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 &quot;Fahrenheit 451&quot; 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 &quot;Rick and Morty&quot;, 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 &quot;Mario&quot; 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&#039;s middle name?",
"correct_answer": "DeForest",
"incorrect_answers": [
"DeWinter",
"Steven",
"Bryce"
]
},
{
"category": "Entertainment: Cartoon & Animations",
"type": "boolean",
"difficulty": "medium",
"question": "In &quot;Avatar: The Last Airbender&quot; and &quot;The Legend of Korra&quot;, Lavabending is a specialized bending technique of Firebending.",
"correct_answer": "False",
"incorrect_answers": [
"True"
]
}
]
}

代码中有很多问题。我不知道你正在使用的所有库,但我会这样做

首先,启动一个
GetStringAsync
,但不等待结果立即继续。我不知道你使用的所有图书馆当然可能是这样的

然而,我宁愿使用统一的

同样,我不知道您的JSON库,但您的类似乎与JSON数据结构不匹配(通过简单地阻塞它)

然后,如注释中所述,注意c#中的数组索引是基于
0
,因此第一个元素是

var firstResult = dataContractDetail.results[0];
现在的问题是你想在你的文本上看到什么?
firstResult
不是
string
,而是一个包含各种成员的类!例如,您可能希望将问题显示为

text.text = firstResult.question;

因为我试图通过执行“results[1]”来调用results中的第一个对象(仅供参考),C#数组是基于零索引的,所以第一个结果将是0而不是1。(我认为这与你的问题无关。)结果json有10个对象,因此结果[8]和结果[0]应该会返回一个字符串。谢谢你详细而有用的回答!你真的为我节省了很多时间。我感谢你的帮助!!
...
MyDetail dataContractDetail = JsonUtility.FromJson<MyDetail>(json);
var firstResult = dataContractDetail.results[0];
text.text = firstResult.question;