Newtonsoft.Json deserialize object in Xamarin iOS project

会有一股神秘感。 提交于 2019-11-27 09:38:03

Just got back to this problem. In case anyone‘s wondering: Turns out the Xamarin Live Player I’ve recently had to use has some problems with Json.NET. (https://developer.xamarin.com/guides/cross-platform/live/limitations/)

I knew it had to be something stupid... Well maybe I can save someone else some time!

Setting iOS Build Linker Behavior to "Link Framework SDKs only" fixed this for me.

As T.J.Purtell.1752 describes in the referenced xamarin thread, the Xamarin Linker will strip out unused types, methods, and fields.

Link Framework SDKs only prevents that happening for my application classes, which is fine in my conmtext.

Thanks a lot to all the responses so far! Sadly none of them worked for me.

After a lot of trial and error I found that I am able to access my data like so:

var blub = JsonConvert.DeserializeObject(booksString);
foreach (var element in (JArray)blub)
{
    var blublub = ((JObject)element).SelectToken("$.Title").ToString();
}

But it would be an absolute pain if I had to implement my whole API this way... Are there any other ideas from you guys?

Please try like this:

Item item = JObject.Parse (json).ToObject<Item> ();

Yes, it's kind of ugly fix, but if it's working, it's working :)

Based on the JSON being returned it would need to be desrialized to an array where the objects map for example

public class Isbn {
    public string Isbn10 { get; set; }
    public string Isbn13 { get; set; }
}

public class Item {
    public Isbn Isbn { get; set; }
    public string Title { get; set; }
    public IList<string> Authors { get; set; }
    public string Publisher { get; set; }
    public string ReleaseDate { get; set; }
    public int PageCount { get; set; }
    public string Description { get; set; }
    public string ThumbnailUrl { get; set; }
}

And then deserialized like

var items = JsonConvert.DeserializeObject<Item[]>(json);

Possible Causes

  1. The deserialized object does not have default constructor .. new Foo()
  2. The constructor contains complex objects such as ICommand, DelegateCommand.
  3. Some properties of the deserialized object is complex objects and consume computing power during serializing, deserializing.
  4. Is not selecting Link Frameword SDKs only
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!