[英]Deserialize JSON into classes fails with “Failure has occurred while loading a type”
I have a REST API, with an endpoint that returns JSON. 我有一个REST API,其端点返回JSON。 I call this API from a Xamarin app (iOS simulator), and I try to run the following call:
我从Xamarin应用程序(iOS模拟器)调用此API,然后尝试运行以下调用:
IEnumerable<Project> projects = null;
HttpResponseMessage response = await _client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var jsonStr = await response.Content.ReadAsStringAsync();
projects = JsonConvert.DeserializeObject<IEnumerable<Project>>(jsonStr);
}
In debugging, I can see that my jsonStr
is perfectly valid, and if I use a service such as http://json2csharp.com/ , my classes are perfect. 在调试中,我可以看到我的
jsonStr
是完全有效的,并且如果我使用http://json2csharp.com/之类的服务,则我的类是完美的。
However, what happens on the DeserializeObject
object, is I get the error: 但是,在
DeserializeObject
对象上发生了什么,我得到了错误:
Failure has occurred while loading a type
加载类型时发生故障
I have 3 projects: 我有3个项目:
All 3 have version 10.0.3
added under "Packages". 所有3个版本均在“软件包”下添加了
10.0.3
版。 Screenshot: 屏幕截图:
However, I keep getting this error (targeting iPhone 7 iOS 10.3.1): 但是,我一直收到此错误(针对iPhone 7 iOS 10.3.1):
Please help! 请帮忙! I'm at the "throwing the computer out of the window" state :D
我处于“将计算机扔出窗外”状态:D
EDITS: 编辑:
AppDelete of iOS app: iOS应用的AppDelete:
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace EesyApp.iOS
{
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}
}
Project.cs class: Project.cs类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Asano.Wrapper.Api.Models
{
public class Project
{
public Customer Customer { get; set; }
public Signee Signee { get; set; }
public int Id { get; set; }
public int CustomerId { get; set; }
public int SigneeId { get; set; }
public int ProjectState { get; set; }
public string DateCreated { get; set; }
public object DateUpdated { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Source { get; set; }
}
public class Customer
{
public object Country { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public string VatNumber { get; set; }
public string Address { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public object Region { get; set; }
public object OfficePhone { get; set; }
public object OfficeEmail { get; set; }
public object CountryId { get; set; }
public string DateCreated { get; set; }
public object DateUpdated { get; set; }
public object Email { get; set; }
}
public class Signee
{
public int Id { get; set; }
public string DateCreated { get; set; }
public object DateUpdated { get; set; }
public object FirstName { get; set; }
public object LastName { get; set; }
public object CompanyName { get; set; }
public object VatNumber { get; set; }
public string Email { get; set; }
public object PhoneNumber { get; set; }
public int SigneeType { get; set; }
public object Address { get; set; }
public object Zipcode { get; set; }
public object City { get; set; }
public object Region { get; set; }
}
}
StackTrace: 堆栈跟踪:
The Offers file calling LeadService: Offers文件调用LeadService:
The method that actually calls the API is callde GetBySpecification and is inside my LeadService. 实际调用该API的方法称为callde GetBySpecification,位于我的LeadService内部。
public partial class Offers : ContentPage
{
public Offers()
{
InitializeComponent();
}
protected async override void OnAppearing()
{
base.OnAppearing();
LeadService leadService = new LeadService();
var items = await leadService.GetBySpecification(new Asano.Wrapper.Api.Models.Specifications.ProjectSpecification());
OfferList.ItemsSource = items;
}
protected override bool OnBackButtonPressed()
{
return true;
}
async void Handle_ItemSelected(object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as Project;
await Navigation.PushAsync(new SingleOffer(item));
//OfferList.SelectedItem = null;
}
}
So my answer isn't really a good answer, but as "I gave up but found a solution" answer. 因此,我的答案并不是一个很好的答案,而是“我放弃但找到了解决方案”的答案。
I tried to test if NewtonSoft.Json actually worked at all. 我试图测试NewtonSoft.Json是否真的起作用。 I tried to do the simplest call you can with this package (turn object into a string).
我尝试使用此包进行最简单的调用(将对象转换为字符串)。 When I did this - and it had nothing to do with my API - it still failed with same error.
当我这样做时-与我的API无关-它仍然失败,并出现相同的错误。 By doing this I knew it was the package that was something wrong with.
通过这样做,我知道是软件包出了问题。
I started a new project, moved my files up and it worked. 我开始了一个新项目,将文件上移并成功运行。
Along the way I learned that you don't have to add the reference to NewtonSoft to the native apps, but just the main forms app and potential helper libraries. 在此过程中,我了解到您不必将对NewtonSoft的引用添加到本机应用程序中,而只需将主要表单应用程序和潜在的帮助程序库添加到。 So not a good and helpful answer, but, well, I've moved on and it works.
因此,这不是一个很好且有用的答案,但是,我已经继续前进,并且有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.