Hi folks,
I have two lots of code which as far as I can see are similar, but only one works.
Working code:
//WheelyThings.cs
[System.Serializable]
public class WheelyThings
{
// Start is called before the first frame update
public string Make;
public string Model;
public int Wheels;
}
//WheelyList.cs
using System.Collections.Generic;
[System.Serializable]
public class WheelyList
{
public List<WheelyThings> Vehicles = new List<WheelyThings>();
}
JSONExample.cs
using System.Collections.Generic;
using UnityEngine;
public class JSONExample : MonoBehaviour
{
public WheelyList VehicleList = new WheelyList();
void Start()
{
TextAsset asset = Resources.Load("Vehicles") as TextAsset;
if(asset != null)
{
VehicleList = JsonUtility.FromJson<WheelyList>(asset.text);
Debug.Log("Count of Vehicles: " + VehicleList.Vehicles.Count);
foreach(WheelyThings vehicle in VehicleList.Vehicles)
{
print(vehicle.Make + " " + vehicle.Model + " " + vehicle.Wheels);
// print(vehicle.Model);
// print(vehicle.Wheels);
}
}
else
{
print("asset is mull");
}
}
}
//WheelyThings.JSON
{
"Vehicles": [
{
"Make": "Toyota",
"Model": "Camry",
"Wheels": 4
},
{
"Make": "Yamaha",
"Model": "YZF-R15",
"Wheels": 2
},
{
"Make": "Ford",
"Model": "Ranger",
"Wheels": 4
}
]
}
This code works. The count is three and the Vehicles are listed. However, the code below does not work:
// Questions.cs
using System.Collections.Generic;
[System.Serializable]
public class Questions
{
public int ID;
public string Category;
public string Question;
public int PointValue;
public string CorrAnswer;
public string altAnswer1;
public string altAnswer2;
public string altAnswer3;
}
QuestionList.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class QuestionList {
public List<Questions> QuestionEntry = new List<Questions>();
}
JSONExample.cs
using System.Collections.Generic;
using UnityEngine;
public class JSONExample : MonoBehaviour
{
public QuestionList qList = new QuestionList();
void Start()
{
TextAsset asset = Resources.Load("QuizMaster") as TextAsset;
if(asset != null)
{
qList = JsonUtility.FromJson<QuestionList>(asset.text);
Debug.Log("Count: " + qList.QuestionEntry.Count);
foreach(Questions questionq in qList.QuestionEntry)
{
print(questionq.CorrAnswer);
print("ooooooo");
}
}
else
{
print("asset is mull");
}
}
}
// QuizMaster.json
{
"Questions" : [
{
"ID": "1",
"Category": "Chess",
"Question": "Who became world chess champion in 1948?",
"PointValue": 500,
"CorrAnswer": "Mikhail Botvinnik",
"altAnswer1": "Robert James Fischer",
"altAnswer2": "Samuel Reshevsky",
"altAnswer3": "Tigran Petrosian"
},
{
"ID": "2",
"Category": "Literature",
"Question": "Who was not one of the original Musketeers?",
"PointValue": 100,
"CorrAnswer": "D'artagnan",
"altAnswer1": "Athos",
"altAnswer2": "Porthos",
"altAnswer3": "Aramis"
},
{
"ID": "3",
"Category": "Literature",
"Question": "To begin with Marley was as dead as a doornail, are the opening lines to what novel?",
"PointValue": 200,
"CorrAnswer": "A Christmas Carol",
"altAnswer1": "The Turn of the Screw",
"altAnswer2": "The Scarlet Pimpernel",
"altAnswer3": "Howard's End"
}
]
}
The count is 0 and no listing is printed. What am I missing? Any help is much appreciated.