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);
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!!
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:
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…