System.NotSupportedException 试图从 Unity3d/Oculus 中的 JSON 反序列化 Vector3

System.NotSupportedException trying to deserialize Vector3 from JSON in Unity3d/Oculus

我目前正在尝试为 Unity(对于虚拟现实项目)反序列化 Vector3[][] 类型的 json 字符串,它抛出了上面的异常。我的json字符串如下:

[[{"x":-2.343096,"y":1.72245443,"z":0.0,"normalized":{"x":-0.805718362,"y":0.592298865,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.9080832,"sqrMagnitude":8.456948},{"x":-2.343096,"y":1.72245443,"z":0.0,"normalized":{"x":-0.805718362,"y":0.592298865,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.9080832,"sqrMagnitude":8.456948},{"x":-2.30125523,"y":1.61087871,"z":0.0,"normalized":{"x":-0.819231868,"y":0.573462367,"z":0.0,"normalized":{"x":-0.8192319,"y":0.5734624,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.00000012},"magnitude":0.99999994,"sqrMagnitude":0.99999994},"magnitude":2.80904,"sqrMagnitude":7.89070559},{"x":-2.24546719,"y":1.4853555,"z":0.0,"normalized":{"x":-0.834037364,"y":0.5517079,"z":0.0,"magnitude":1.0,"sqrMagnitude":0.99999994},"magnitude":2.692286,"sqrMagnitude":7.248404},{"x":-2.21757317,"y":1.38772666,"z":0.0,"normalized":{"x":-0.8476986,"y":0.5304781,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.61599231,"sqrMagnitude":6.843416},{"x":-2.17573214,"y":1.27615035,"z":0.0,"normalized":{"x":-0.8625732,"y":0.5059323,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.522374,"sqrMagnitude":6.36237},{"x":-2.147838,"y":1.17852151,"z":0.0,"normalized":{"x":-0.8766962,"y":0.481044352,"z":0.0,"magnitude":1.0,"sqrMagnitude":0.99999994},"magnitude":2.4499228,"sqrMagnitude":6.00212145},{"x":-2.09205,"y":0.997210741,"z":0.0,"normalized":{"x":-0.90269357,"y":0.430284023,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.317564,"sqrMagnitude":5.371103},{"x":-2.05020928,"y":0.829846859,"z":0.0,"normalized":{"x":-0.926946759,"y":0.375192851,"z":0.0,"magnitude":1.0,"sqrMagnitude":0.99999994},"magnitude":2.21178746,"sqrMagnitude":4.892004},{"x":-2.02231526,"y":0.6764293,"z":0.0,"normalized":{"x":-0.9483558,"y":0.3172085,"z":0.0,"magnitude":1.0,"sqrMagnitude":0.99999994},"magnitude":2.13244367,"sqrMagnitude":4.5473156},{"x":-1.99442124,"y":0.5230123,"z":0.0,"normalized":{"x":-0.967293262,"y":0.253660679,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.061858,"sqrMagnitude":4.251258},{"x":-1.99442124,"y":0.383542776,"z":0.0,"normalized":{"x":-0.982006431,"y":0.1888475,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.03096557,"sqrMagnitude":4.124821},{"x":-1.980474,"y":0.230125785,"z":0.0,"normalized":{"x":-0.99331665,"y":0.115420744,"z":0.0,"normalized":{"x":-0.9933167,"y":0.115420751,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":0.99999994,"sqrMagnitude":0.99999994},"magnitude":1.99379921,"sqrMagnitude":3.97523522}],[{"x":-0.9344492,"y":-4.748954,"z":0.0,"normalized":{"x":-0.193067357,"y":-0.9811855,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":4.84001637,"sqrMagnitude":23.4257584},{"x":-0.9344492,"y":-4.748954,"z":0.0,"normalized":{"x":-0.193067357,"y":-0.9811855,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":4.84001637,"sqrMagnitude":23.4257584}],[{"x":0.7112968,"y":-4.66527176,"z":0.0,"normalized":{"x":0.1507245,"y":-0.988575757,"z":0.0,"normalized":{"x":0.150724515,"y":-0.9885758,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":0.99999994,"sqrMagnitude":0.9999999},"magnitude":4.719185,"sqrMagnitude":22.2707043},{"x":0.7112968,"y":-4.66527176,"z":0.0,"normalized":{"x":0.1507245,"y":-0.988575757,"z":0.0,"normalized":{"x":0.150724515,"y":-0.9885758,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":0.99999994,"sqrMagnitude":0.9999999},"magnitude":4.719185,"sqrMagnitude":22.2707043}]]

我正在使用代码:

Vector3[][] allLines = JsonConvert.DeserializeObject<Vector3[][]>(jsonString);

为什么经常出现这种异常?

Exception Screenshot

System.NotSupportedException: System.Reflection.Emit.DynamicMethod::ctor
at System.Reflection.Emit.DynamicMethod..ctor (System.String name; System.Type returntype. System.Type[] paremeterTypes, System.Type [...]
at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDynamicMethod [...]
At "" CreatenewLoist
At "" CreateList
At "" CreateValueInternal
At "" Desiarilize
At "" DeserializeInternal
at "" DeserializeObject
at "" DeserializeObject[T]
at Draw.Start

好的伙计们。有效。所以我完全按照 post 所说的做了 @dbc 链接:

  1. 得到\unityprojects\football\Packages\Newtonsoft.Json.13.0.1\lib\net20\Newtonsoft.json.dll 文件 其中“足球”是项目名称。

  2. 放入unityproject中的文件夹\assemblies\AOT文件夹

  3. 创建“link.xml”文件并将其放入assets文件夹,内容如下:

         <linker>
         <assembly fullname="Your.Dto.Package.Name" preserve="all" />
         <assembly fullname="System.Core">
             <type fullname="System.Linq.Expressions.Interpreter.LightLambda" preserve="all" />
         </assembly></linker>
    

希望对遇到同样问题的人有所帮助。