0

I've tried other peoples solutions found online with JsonFX but I keep getting the same error.

InvalidCastException: Cannot cast from source type to destination type. Pathfinding.Serialization.JsonFx.JsonReader.Deserialize[Child[]] (System.String value) GetData+c__Iterator2.MoveNext () (at assets/GetData.cs:37)

I retrieve a JSON string from a webpage, (e.g. an array with 2 elements)

[ {"id":"1","age":"1","teeth":"4","url":"http://hidden","father":"John","mother":"Jaime","childFk":"1"},{"id":"2","age":"2","teeth":"7","url":"http://hidden","father":"Zach","mother":"Briann","childFk":"1"} ]

My code

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using Pathfinding.Serialization.JsonFx;

public class Child {

    public int id;
    public int age;
    public int teeth;
    public string url;
    public string father;
    public string mother;
    public int childFk;
}

public class GetData : MonoBehaviour {

    public string url = "https://hiddenPage";
    public Text exampleText;
    public string jsonString;

    void Start()
    {
        StartCoroutine(getJSON());
    }

    IEnumerator getJSON() {
        WWW www = new WWW(url);
        yield return www;

        jsonString = www.text;
        exampleText.text = jsonString;
        Debug.Log(jsonString);

        Child[] testData = JsonReader.Deserialize<Child[]>(jsonString); //LINE 37
        foreach(var row in testData){
            Debug.Log(row.father);
        }
        Debug.Log(testdata);
    }
}

Please help!

0 Answers0