在Unity(2D阵列)中获取JSON数据

时间:2019-06-12 04:59:40

标签: c# json unity3d vuforia

我对Unity和C#非常陌生。我正在尝试将JSON字符串加载到Unity,并且一切正常,但是当我想加载2D数组时,它根本无法工作。

我有这个...

{
    "status": 0, 
    "email": "exmpl@u", 
    "questions": [
        {
            "A": "Faro", 
            "C": "Oporto", 
            "B": "Lisbon", 
            "D": "Coimbra", 
            "question": "Which is the capital of Portugal?", 
            "your_answer": "", 
            "points": 10, 
            "correct_answer": "B"
        }, 
        {
            "A": "Seville", 
            "C": "Madrid", 
            "B": "Barcelona", 
            "D": "C\u00e1ceres", 
            "question": "Which is the capital of Spain?", 
            "your_answer": "", 
            "points": 15, 
            "correct_answer": "C"
        }, 
        {
            "A": "Gigon", 
            "C": "Cannes", 
            "B": "Marselle", 
            "D": "Paris", 
            "question": "Which is the capital of France?", 
            "your_answer": "", 
            "points": 20, 
            "correct_answer": "D"
        }
    ]
}

...或此字符串

{
    "status": 0, 
    "email": "exmpl@u", 
    "questions": [
        {
            "A": "Marcelo Rebelo de Sousa", 
            "C": "M\u00e1rio Soares", 
            "B": "Anibal Cavaco Silva", 
            "D": "Jorge Sampaio", 
            "question": "Which is the president of Portugal?",
            "your_answer": "", 
            "points": 10, 
            "correct_answer": "A"
        }, 
        {
            "A": "Fran\u00e7ois Miterrand", 
            "C": "Jacques Chirac", 
            "B": "Emmanuel Macron", 
            "D": "Nicolas Sarkozy", 
            "question": "Which is the president of France?", 
            "your_answer": "", 
            "points": 15, 
            "correct_answer": "B"
        }
    ]
}

我创建了一个QuizData类,用于将所有对象保存在其中,如下所示

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace QuizDataManager
{
    public class QuizData
    {
        public int status;
        public string email;
        public string[] questions;
    }
}

我可以轻松访问变量状态并通过电子邮件发送myQuizDataObject.status或myQuizDataObject.email,但myQuizDataObject.questions [0]或类似内容什么也收不到。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

我们没有看到您将JSON字符串完全解析到QuizData实例中的情况,但我假设您正在使用JsonUtility

您的c#数据结构不代表您得到的结构。可以简单地生成现有Json字符串的c#布局的一个不错的工具是json2csharp。仅对于Unity,请确保删除所有添加的{get; set;},这些属性将代替使用JsonUtility未(反序列化)的属性。

那会是

[Serializable]
public class QuizData
{
    public int status;
    public string email;
    public Question[] questions;
}

[Serializable]
public class Question
{
    public string A;
    public string C;
    public string B;
    public string D;

    public string question;
    public string your_amswer;
    public int points;
    public string correct_answer;
}

然后,您只需访问例如

var myQuizDataObject = JsonUtility.FromJson<QuizData>(theJsonString);

var firstQuestion = myQuizDataObject.questions[0].question;
var answerA = myQuizDataObject.questions[0].A;
...

根据您的需求而不是string,您也可以像{p>那样为correct_answeryour_answer设置适当的枚举。

public enum AnswerOption
{
    None,
    A,
    B,
    C,
    D
}

[Serializable]
public class Question
{
    public string A;
    public string C;
    public string B;
    public string D;

    public string question;
    public AnswerOption your_amswer;
    public int points;
    public AnswerOption correct_answer;
}

,甚至可以像其他答案中建议的那样自动用字典填充字典。实施ISerializationCallbackReceiver interface

[Serializable]
public class Question : ISerializationCallbackReceiver
{
    public readonly Dictionary<AnswerOption, string>() answers;

    public string A;
    public string C;
    public string B;
    public string D;

    public string question;
    public AnswerOption your_amswer;
    public int points;
    public AnswerOption correct_answer;

    public void OnBeforeSerialize()
    {
        // do nothing but required by the interface
    }

    public void OnAfterDeserialize()
    {
        answers = new Dictionary<AnswerOption, string>(4);

        _myDictionary.Add(AnswerOption.A, A);
        _myDictionary.Add(AnswerOption.B, B);
        _myDictionary.Add(AnswerOption.C, C);
        _myDictionary.Add(AnswerOption.D, D);
    }
}

答案 1 :(得分:1)

我认为myQuizDataObject.questions[0][A] (Guessing)会得到结果,您可以看到像"A": "Faro"这样的数据解析,它是键和值。但是您的数据没有多大意义。你从哪里得到的?

它可能是Dictionary<string, string>

的数组

This似乎是您需要的问题

{
    "status": 0, 
    "email": "exmpl@u", 
    "questions": [{
        "A": "Marcelo Rebelo de Sousa", 
        "C": "M\u00e1rio Soares", 
        "B": "Anibal Cavaco Silva", 
        "D": "Jorge Sampaio", 
        "question": "Which is the president of Portugal?", 
        "your_answer": "", 
        "points": 10, 
        "correct_answer": "A"
    }, {
        "A": "Fran\u00e7ois Miterrand", 
        "C": "Jacques Chirac", 
        "B": "Emmanuel Macron", 
        "D": "Nicolas Sarkozy", 
        "question": "Which is the president of France?", 
        "your_answer": "", 
        "points": 15, 
        "correct_answer": "B"
    }]
}