I'm trying to deserialize a json into a list of dictionaries using JsonFx in unity, but I get this error:
InvalidCastException: Cannot cast from source type to destination type.
My json looks like this:
[{"year":"2015","quarter":"1","sales":"543"},
{"year":"2015","quarter":"2","sales":"638"},
{"year":"2015","quarter":"3","sales":"893"},
{"year":"2016","quarter":"1","sales":"1328"},
{"year":"2016","quarter":"2","sales":"1893"},
{"year":"2016","quarter":"3","sales":"2023"},
{"year":"2017","quarter":"1","sales":"1632"},
{"year":"2017","quarter":"2","sales":"1284"},
{"year":"2017","quarter":"3","sales":"982"}]
And the code:
[Serializable]
public class Response
{
public List<Dictionary<string, object>> Data;
}
public class DatabaseManager : MonoBehaviour
{
public Response Response { get; private set; }
private void Awake()
{
Response = new Response();
StartCoroutine(RequestCorutine(table));
}
private IEnumerator RequestCorutine(string table)
{
// Hago una consulta a la database
string url = "http://maximuller11.000webhostapp.com/ChartTest.php?
table=" + table;
var req = new WWW(url);
yield return req;
Debug.Log("JSON: " + req.text);
// Guardo el resultado de la consulta y la parseo en una clase
var response =
JsonFx.Json.JsonReader.Deserialize<List<Dictionary<string, object>>>
(req.text);
Response.Data = response;
}
I have already tried using Newtonsoft.NET (Json.NET) and it worked, but not in IOS, so I decided to give an oportunity to JsonFX. The line for getting the response in Newtonsoft is:
var response = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(req.text);