0

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);
Maxi
  • 25
  • 1
  • 5
  • I've mentioned to someone about Newtonsoft not working on iOS in some instances few days ago and they argued with me for the whole. Hopefully this person will see this post. 1.Your Json is **not** a dictionary. 2.Your json does not match your `class` that you use to deserialze it. Paste your json [here](http://json2csharp.com/) and it will show you the `class` you need to deserialze it. See the duplicate for how to deseriaze json. Don't forget to remove the `{ get; private set; }` from each `class`. – Programmer Sep 04 '17 at 19:27
  • @Programmer - Re Json.NET, a claim was made in [Xamarin iOS broken #1260](https://github.com/JamesNK/Newtonsoft.Json/issues/1260) that the Portable40 build works on IOS. No idea if it's true though. – dbc Sep 04 '17 at 19:33
  • Well, this is Unity not Xamarin and the Newtonsoft.NET I believe OP is talking about is [this](https://github.com/SaladLab/Json.Net.Unity3D) one which was made to work with Unity. Maybe the Xamarin version is working on Unity too. – Programmer Sep 04 '17 at 19:36
  • I understand, but it doesn't work for me. I need a generic parse, not a static class. I need to receive information and parse it into a dictionary, because I won't know what information is comming, this json is only an example I'm working in. – Maxi Sep 04 '17 at 19:37
  • 1
    If you want to deserialize without a class, use `SimpleJSON` which is mentioned and linked to in the duplicate. See **3.Deserialize json string without class && De-serializing Json with numeric properties** from the duplicate. – Programmer Sep 04 '17 at 19:40

0 Answers0