问题
I am trying to use DataContractJsonSerializer but I can't seem to get a reference/namespace for it. I created my app on a .NET Framework 4.5.
When I click add reference I only get the version 2.0.5.0 for System.ServiceModel
回答1:
If you want to get DataContractJsonSerializer working with Android you would need to create a PCL project and then it would be available under namespace System.Runtime.Serialization.Json. This would require a Xamarin business license or higher to work.
Better option would be to use Json.NET (much faster than DataContractJsonSerializer if you use it more than once) or ServiceStack.Text (much faster than Json.NET). XLabs offers an abstraction on mobile compatible serializers (Nuget, search for XLabs.Serialization) and has plugins for Json.NET, ServiceStack.Text (v3), ProtoBuf & of course DataContractSerializer (.NET XML serializer). DataContractJsonSerializer is omitted from the package since it requires business license and really doesn't offer much over Json.NET or ServiceStack.
回答2:
DataContractJsonSerializer will not work with Xamarin. I use Newtonsoft.Json nuget package to work with json serialization and till now haven't found any problem with it. It's very simple:
string str = Newtonsoft.Json.JsonConvert.SerializeObject(myObject);
来源:https://stackoverflow.com/questions/29539221/xamarin-using-datacontractjsonserializer-for-android-blank-app