there are various ressources on the web explaining how to serialize some C# object to a JSON string. I have been unable to get any of them to work with MonoTouch 3.2.4 though.
Alternatives I came across:
the System.Json Namespace
I am already successfully using the System.Json namespace to de-serialize data. Hence, for simplicity's sake, I'd prefer to stick to that if possible. Unfortunately, I have no idea how to serialize with it (or if it is even possible).System.Runtime.Serialization.Json.DataContractJsonSerializer
The System.Runtime.Serialization.Json namespace is not available in MonoTouch. I've read somewhere that it is part of .NET 3.5 and that MonoTouch is at .NET 2.0 level. That might explain it.System.Web.Script.Serialization.JavaScriptSerializer
This one is also missing in MonoTouch (in fact, there only seems to be System.Web.Services available).Json.NET by James Newton-King
I managed to make this one work on the simulator by including the .NET 2.0 assembly dll and using Newtonsoft.Json.JsonConvert.SerializeObject(). However, this doesn't compile for the device with error "mtouch failed with no output". As soon as I remove the assembly reference, it compiles fine.
Is there any way to make one of those work with MonoTouch? Are there other alternatives to try?
Thanks
I don't have street cred, so I can't post a comment to the answer above..
Just wanted to post a warning when using the System.Json.JsonObject.ToString() method. I was using this, and it seemed to generate JSON fine.. then I had a string with a return (\n) in it. It didn't escape this properly, and it ended up generating bad JSON..
So, if you have simple JSON, it may work just fine, but if you know you may have special characters in your strings that require escaping, you should test that "ToString()" method first.
I think the newtonsoft JSON library looks like an alternative. There is info about this over at Xamarin's site:
https://components.xamarin.com/view/json.net
Realize this is an awful answer, but just wanted to post the warning to users of the system json lib.
I'd recommend using the System.Json library, which is not too difficult to use and wrap in a class to make it strongly typed. It is also included as a default assembly with MonoTouch as it was inherited from Silverlight.
To load it up:
Here is an example by index:
One my name:
Where this is a class with a string property named StringValue and an int property named IntValue. For the most part, the JsonValue class will cast to your types implicitly.
So what I do, is create a class to hold all of the Json info in a strongly typed way, then pass in a JsonValue in the constructor to do the ugly weakly typed stuff.
For other libraries, you will run into problems where MonoTouch strips out types that are seen to be "unused" when created by reflection (as when you use XmlSerializer, for example). I have found myself fighting that issue more than necessary, having to add PreserveAttribute on types, etc.
On this issue, read here for more info on Preserve: MonoTouch Doc