InvalidCastException When Deployed to Hololens (but runs fine in Unity)
I created a testing app to visualize some JSON data. It ran fine in Unity without error, but when deployed to Hololens it gave me the following error:
"InvalidCastException: Unable to cast object of type 'Wrapper'1[p0] to type 'UnityEngine.IUnitySerializable'."
The "Wrapper" and "p0" are two classes I created to parse JSON. I suspect there might be something wrong in the way I wrote them, but couldn't understand why Unity won't show the error. Attach is the code:
public class CFD_JSON : MonoBehaviour
{
private List<p0> cfd = new List<p0>();
IEnumerator Start()
{
string url = "http://140.247.162.185:9000/point";
WWW www = new WWW(url);
yield return www;
if (www.error != null)
{
Debug.Log("error");
}
else
{
cfd = ArrayJson.getJsonArray<p0>(www.text);
...*some actions*
}
}
}
...
[System.Serializable]
public class ArrayJson
{
public static List<p0> getJsonArray<T>(string json)
{
string newJson = "{ \"array\": " + json + "}";
Wrapper<p0> wrapper = JsonUtility.FromJson<Wrapper<p0>>(newJson);
return wrapper.array;
}
[System.Serializable]
private class Wrapper<p0>
{
public List<p0> array = new List<p0>();
}
}
[System.Serializable]
public struct p0
{
public float x;
public float y;
public float z;
public float vmeg;
public float vx;
public float vy;
public float vz;
}
Many thanks!
Answer by hwah · Oct 17, 2017 at 02:04 PM
I have the exact same issue when I tried to wrap a JSON string in HoloLens.....
Answer by Coulyd · Jan 17, 2018 at 02:07 PM
Hello
You have to use the dll Newtonsoft (inlcued in Holotoolkit).
import Newtonsoft;
...
MyClass data = Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(jsonString);
Your answer
Follow this Question
Related Questions
Using System.Runtime.Serialization.Formatters.Binary for Windows Store Apps 3 Answers
How to resolve Supported API test failure in Windows App Certification Kit - Test Results 0 Answers
InvalidCastException: Cannot cast from source type to destination type. 1 Answer
Loading content at runtime with HoloLens and IL2CPP 1 Answer
dynamic[] args become System.Object[] args when built to UWP and cause crashes 0 Answers