Newtonsoft.Json 反序列化 Xamarin iOS 项目中的对象 [英] Newtonsoft.Json deserialize object in Xamarin iOS project

查看:30
本文介绍了Newtonsoft.Json 反序列化 Xamarin iOS 项目中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Xamarin.Forms 项目出现了一些非常奇怪的行为.

我正在从 REST API 检索一些序列化数据.然后我尝试使用 Json.NET 将其反序列化为一个对象.

这在 Android 上运行良好,如果我将代码复制到 .NET 控制台应用程序,它也可以运行.但是,在我的 iOS 项目中,对 .DeserializeObject() 的调用仅返回 null.它不会以任何方式抛出错误或抱怨.

我确实找到了这个讨论:https://forums.xamarin.com/discussion/15152/deserialization-not-working-with-json-net-on-ios-device-ios-simulator-works,但我没有首先链接所有程序集并尝试设置 TJPurtell.1752 建议的属性也没有帮助.

谁能告诉我在这里做什么?

非常感谢!

一月

编辑

这是我尝试反序列化的示例响应:

<预><代码>[{伊斯本":{"Isbn10":"0099910101","Isbn13":"9780099910107"},《标题》:《告别武器》,作者":[海明威"],"Publisher":"兰登书屋","发布日期":"1994","PageCount":293,描述":1918 年欧内斯特·海明威参战,参加'结束所有战争的战争'.他自愿在意大利参加救护车服务,受伤并两次接受装饰.从他的经历中得到了告别武器.海明威对他的描述战争是令人难忘的.他以完全的信念重现了他年轻的美国志愿者的恐惧、同志情谊、勇气以及他在意大利遇到的男人和女人.但《告别武器》不仅是一部战争小说.海明威也在其中创造了一个充满戏剧性和不妥协激情的爱情故事.","ThumbnailUrl":"http://books.google.com/books/content?id=m68LhBiNv8YC&printsec=frontcover&img=1&zoom=1&source=gbs_api"},{伊斯本":{"Isbn10":"9044538780","Isbn13":"9789044538786"},"Title":"Het huis van de namen",作者":[《科尔姆·托宾》],"Publisher":"Singel Uitgeverijen","发布日期":"2017-08-24","PageCount":0,描述":Klytaimnestra heeft veel moeten doorstaan​​.Haar man heeft haar kind, haar mooie oudste dochter, geofferd aan de goden, en zij heeft hem niet kunnen tegenhouden.kinderen reageren op deze ontwrichtende gebeurtenis?Eénding neemt ze zich voor:haar man zal dit met de dood moeten bekopen.","ThumbnailUrl":"http://books.google.com/books/content?id=yl8yDwAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"}]

解决方案

刚刚回到这个问题.如果有人想知道:结果我最近不得不使用的 Xamarin Live Player 在 Json.NET 方面存在一些问题.(https://developer.xamarin.com/guides/cross-platform/直播/限制/)

我知道这一定是愚蠢的……好吧,也许我可以节省一些时间!

I'm getting some very weird behaviour in my Xamarin.Forms Project.

I'm retrieving some serialized data from a REST API. I'm then trying to use Json.NET to deserialize that to an object.

This works perfectly fine on Android and if I copy the code to a .NET console application it also works. However in my iOS project, the call to .DeserializeObject() just returns null. It doesn't throw an error or complain in any way.

I did find this discussion: https://forums.xamarin.com/discussion/15152/deserialization-not-working-with-json-net-on-ios-device-ios-simulator-works, but I didn't link all assemblies in the first place and trying to set the attribute suggested by T.J.Purtell.1752 didn't help either.

Could anyone tell me what to do here?

Thanks a lot!

Jan

EDIT

Here's a sample response of what I'm trying to deserialize:

[
   {
      "Isbn":{
         "Isbn10":"0099910101",
         "Isbn13":"9780099910107"
      },
      "Title":"A Farewell to Arms",
      "Authors":[
         "Ernest Hemingway"
      ],
      "Publisher":"Random House",
      "ReleaseDate":"1994",
      "PageCount":293,
      "Description":"In 1918 Ernest Hemingway went to war, to the 'war to end all wars'. He volunteered for ambulance service in Italy, was wounded and twice decorated. Out of his experiences came A Farewell to Arms. Hemingway's description of war is unforgettable. He recreates the fear, the comradeship, the courage of his young American volunteer and the men and women he meets in Italy with total conviction. But A Farewell to Arms is not only a novel of war. In it Hemingway has also created a love story of immense drama and uncompromising passion.",
      "ThumbnailUrl":"http://books.google.com/books/content?id=m68LhBiNv8YC&printsec=frontcover&img=1&zoom=1&source=gbs_api"
   },
   {
      "Isbn":{
         "Isbn10":"9044538780",
         "Isbn13":"9789044538786"
      },
      "Title":"Het huis van de namen",
      "Authors":[
         "Colm Tóibín"
      ],
      "Publisher":"Singel Uitgeverijen",
      "ReleaseDate":"2017-08-24",
      "PageCount":0,
      "Description":"Klytaimnestra heeft veel moeten doorstaan. Haar man heeft haar kind, haar mooie oudste dochter, geofferd aan de goden, en zij heeft hem niet kunnen tegenhouden. Hoe kan ze dit ooit te boven komen? En hoe zullen haar andere twee kinderen reageren op deze ontwrichtende gebeurtenis? Eén ding neemt ze zich voor: haar man zal dit met de dood moeten bekopen.",
      "ThumbnailUrl":"http://books.google.com/books/content?id=yl8yDwAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
   }
]

解决方案

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!

这篇关于Newtonsoft.Json 反序列化 Xamarin iOS 项目中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆