- Home /
Instantiate a prefab from jsonarray?
I'm using php and mysql to get a json string from the database via the www and using the simplejson library. From what I understand the www comes in as a string. What I want to do is convert it to a jsonarray, which I did: WWW VehicleData = new WWW ("http://localhost/CMVM/ViewVehicle.php", VehicleDataForm);
yield return VehicleData;
string VD = VehicleData.text;
JSONArray VehicleArray = JSON.Parse (VD).AsArray;
I did a debug on it and it works very well. Now I need to instantiate my prefab with text components, but I only know how to do that using a list
for(int i = 0; i< VehicleArray.Count; i++)
{
GameObject vObj = Instantiate(VehiclePrefab);
VehicleIndex tmpVehicle = VehicleArray[i];
vObj.GetComponent<VehicleIndexScript> ().DisplayVehicles ("ID:" + tmpVehicle.ID, "Vehcile Make: " + tmpVehicle.Make, "Vehicle Model:" + tmpVehicle.Model, "Vehicle Year: " + tmpVehicle.Year, "Vehicle Mileage:" + tmpVehicle.Mileage + tmpVehicle.Location, "Location:");
vObj.transform.SetParent(DatabasedisplayParent);
}
I tried just using the array, but it gave me a conversion error: error CS0029: Cannot implicitly convert type SimpleJSON.JSONNode' to
VehicleIndex'
What's the best way to proceed? Do I try to convert the jsonarray to a list? If so how do I do that? linq? Do I try to instantiate using the jsonarray? if so how do I do that?
Thanks!!
Create a serializable class for your VehicleData
array.
[Serializable]
public void VehicleDataArray{
public VehicleData[] array;
}
Instantiate a new VehicleDataArray and add all your VehicleData
instances to array
(i assume your VehicleData is also [Serializable]. Cause it must be). Then do following to convert it to json array and save the result:
var jsonArray = JsonUtility.ToJson([your VehicleDataArray instance]);
//save jsonArray
When you want to retrieve data just do following:
//load your jsonArray text file again
var vehicleDataArray = JsonUtility.FromJson<VehicleDataArray>(jsonArray);
Now you can reach your each VehicleData
from vehicleDataArray.array
. Just use a for loop or whatever you want...
Answer by Bunny83 · Mar 20, 2019 at 10:18 PM
What does your json actually look like? Give an example. JSON is just an object notation. Keep in mind that SimpleJSON is not an object deserializer. It just provides the json data in a structured manner. You don't need any extra classes for your json data when using SimpleJSON. Just access the fields you want to access.
Just from your code fragments i guess your json would look something like this:
[
{
"ID" : 1,
"Make" : "aaaa",
"Model" : "bbbb",
"Year" : "cccc",
"Mileage" : "dddd",
"Location" : "eeee"
},
{
"ID" : 2,
"Make" : "aaaa",
"Model" : "bbbb",
"Year" : "cccc",
"Mileage" : "dddd",
"Location" : "eeee"
}
]
If that's the case you would read that data like this:
var vehicles = JSON.Parse(jsonText);
foreach(JSONNode v in vehicles)
{
GameObject vObj = Instantiate(VehiclePrefab);
vObj.GetComponent<VehicleIndexScript> ().DisplayVehicles (
"ID:" + v["ID"].AsInt,
"Vehcile Make: " + v["Make"].Value;
"Vehicle Model:" + v["Model"].Value,
"Vehicle Year: " + v["Year"].Value,
"Vehicle Mileage:" + v["Mileage"].Value,
"Location:"+ v["Location"].Value
);
}
This assumes you're using the latest version from github.
If your actual json data looks different, please edit your question and include an example
In this case, the ID is a string, so I changed it to Value. In order for the id to show up, the index has to read VehicleID.Value. It is working! Thanks Bunny!