Can't use .NET's JavaScriptSerializer in U

2019-07-17 20:28发布

问题:

I'm trying to use .NET's System.Web.Script.Serialization.JavaScriptSerializer to read a JSON file into a Dictionary in Unity with MonoDevelop on MacOS.

So far I've added the System.Web.Extensions assembly to MonoDevelop which makes it build the "Assembly-CSharp" project fine.

But when I go back to Unity, it still shows the error: error CS0234: The type or namespace name `Web' does not exist in the namespace `System'. Are you missing an assembly reference?

Then I read on the web that adding the DLL anywhere into the assets folder would do, so I found some versions of the file System.Web.Extensions.dll inside MonoDevelop's path, they are in: /Applications/Unity/MonoDevelop.app/Contents/Frameworks/Mono.framework/Versions/2.10.2/lib/mono/gac/System.Web.Extensions

There are 3 folders with different versions of the file: 1.0.61025.0__31bf3856ad364e35, 3.5.0.0__31bf3856ad364e35 and 4.0.0.0__31bf3856ad364e35

Tried all of them, but none works, Unity gives this error:

Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
File name: 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
  at (wrapper managed-to-native) System.Reflection.Assembly:InternalGetType (System.Reflection.Module,string,bool,bool)
  at System.Reflection.Assembly.GetType (System.String name, Boolean throwOnError, Boolean ignoreCase) [0x00000] in <filename unknown>:0 
  at System.Reflection.Assembly.GetType (System.String name) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.RootNamespace.GetTypeInAssembly (System.Reflection.Assembly assembly, System.String name) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.RootNamespace.LookupTypeReflection (Mono.CSharp.CompilerContext ctx, System.String name, Location loc, Boolean must_be_unique) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.GlobalRootNamespace.LookupTypeReflection (Mono.CSharp.CompilerContext ctx, System.String name, Location loc, Boolean must_be_unique) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Namespace.LookupType (Mono.CSharp.CompilerContext ctx, System.String name, Location loc) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Namespace.Lookup (Mono.CSharp.CompilerContext ctx, System.String name, Location loc) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.TypeManager.CoreLookupType (Mono.CSharp.CompilerContext ctx, System.String ns_name, System.String name, Kind type_kind, Boolean required) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.TypeManager.InitCoreTypes (Mono.CSharp.CompilerContext ctx) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0   

So, what now?

回答1:

Unity uses an old version of Mono that does not support .NET 4. I believe they also remove System.Web.dll to slim it down, since System.Web mostly contains ASP.NET which isn't really relevant to game developers.

You might be able to copy over Mono's dlls, however make sure you use v2.0 of System.Web.dll and v3.5 of System.Web.Extensions.dll.

Alternatively you might try JSON.NET.



回答2:

Try Serializing your data, BinaryFormatter + MemoryStream + TextAsset

BinaryFormatter + MemoryStream: object <==> byte[]

TextAsset.bytes is used for binary data file.