I have pretty much completed by app using Xamarin.Android and am now building up the iOS version. I use JSON.NET in my business layer to deserialize JSON from web requests. Works great on Android and the iOS simulator but does not work on my iPhone. I get the problem when I execute the exact same code as I use for Android or run on the iOS simulator.
At first I was getting the "Unable to find a constructor to use for type..." exception. I found if I did a new on the object first, I wouldn't get that error but the deserialization didn't work. This seems like an obvious clue that something is messed up. I've spent enough time on this now that it must be something really stupid. (on my part!)
Here is the test code I am using and the class I am deserializing. Any help would be greatly appreciated as I don't want to have to pluck the properties out one by one.
using System;
namespace TestUserNS
{
public class TestUser
{
public TestUser ()
{
}
public string name { get; set; }
public int id { get; set; }
public string number { get; set; }
}
}
private void TestDeserialize()
{
try {
string json = "{\"status\":\"SUCCESS\",\"user\":{\"id\":185,\"name\":\"Joe S\",\"number\":\"15555555555\"}}";
JObject jsonResponset = JObject.Parse (json);
JObject user_json = (JObject)jsonResponset ["user"];
if (user_json != null) {
Console.WriteLine("json = {0}", user_json.ToString());
TestUserNS.TestUser testUserTemp = new TestUserNS.TestUser();
TestUserNS.TestUser testUser = user_json.ToObject<TestUserNS.TestUser> ();
Console.WriteLine ("id = {0} name = {1} phone = {2}", testUser.id, testUser.name, testUser.number);
// Output: id = 0 name = phone =
int id = (int)user_json["id"];
string name = (string)user_json["name"];
string number = (string)user_json["number"];
Console.WriteLine ("id = {0} name = {1} phone = {2}", id, name, number);
// Output: id = 185 name = Joe S phone = 15555555555
}
} catch (Exception e) {
Console.WriteLine ("e = {0}", e.Message);
}
}