将JSON反序列化为类失败,并显示“加载类型时发生失败”

时间:2017-08-22 14:24:33

标签: c# xamarin dll xamarin.ios json.net

我有一个REST API,其端点返回JSON。我从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);
}

在调试中,我可以看到我的jsonStr完全有效,如果我使用http://json2csharp.com/之类的服务,我的课程就完美了。

然而,DeserializeObject对象上发生了什么,我得到错误:

  

加载类型

时发生了故障

我有3个项目:

  • App.ApiWrapper(便携式类库)
  • App
  • App.iOS

所有3个版本都在“套餐”下添加了版本10.0.3。截图:

enter image description here

但是,我一直收到此错误(定位到iPhone 7 iOS 10.3.1):

enter image description here

请帮忙!我正在“把电脑扔到窗外”状态:D

编辑:

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类:

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; }
    }


}

堆栈跟踪:

enter image description here

调用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;
        }


    }

1 个答案:

答案 0 :(得分:0)

所以我的回答并不是一个很好的答案,但是我已经放弃但是找到了一个解决方案&#34;答案。

我试着测试NewtonSoft.Json是否真的起作用了。我尝试用这个包进行最简单的调用(将对象转换为字符串)。当我这样做 - 而且它与我的API无关 - 它仍然失败并出现同样的错误。通过这样做,我知道包装出了问题。

我开始了一个新项目,将我的文件移动起来并且有效。

在此过程中,我了解到您不必将对NewtonSoft的引用添加到本机应用程序中,而只需要添加主要表单应用程序和潜在的帮助程序库。所以,这不是一个好的和有用的答案,但是,我已经继续前进并且有效。