json - 使用 Newtonsoft JSON 反序列化的 System.TypeLoadException

标签 json serialization xamarin.ios json.net

我真的被难住了。这是我返回的 JSON:

{"ResponseData":[{"ClusterID":"c02f1f5c-c61b-4f2c-ab5a-249966b3cdef","ClusterName":"Northeast","Courses":[{"CourseID":"8ab4f2b3-8160-4d7e-b79f-8d8b58926cc0","CourseName":"Home Course","SubCourses":[{"SubCourseName":"SubCourse1","SubCourseNumber":18}]},{"CourseID":"b3223464-333b-4c54-89c2-23908e0510c9","CourseName":"Away Course","SubCourses":[{"SubCourseName":"SubCourse1","SubCourseNumber":19}]}],"IsHomeCluster":true},"ResponseErrors":[]}

这是我要反序列化的代码:

        JArray jArr = (JArray)JsonConvert.DeserializeObject(json);
        foreach (var item in jArr) {
            foreach (var subitem in item["ResponseData"]) {
                Console.WriteLine (subitem ["ClusterID"]);
            }
        }

项目编译正常,但是当我在模拟器中运行它时,出现此错误:

System.TypeLoadException: A type load exception has occurred. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x00000] in :0 at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in :0 at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in :0 at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in :0 at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value) [0x00000] in :0 at AppMultiView.CourseInformationScreen.ViewDidLoad () [0x00029] in /Users/Dan/Desktop/AppTouch_dev /Screens/CourseInformationScreen.cs:48 at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_bool (intptr,intptr,intptr,bool) at MonoTouch.UIKit.UINavigationController.PushViewController (MonoTouch.UIKit.UIViewController viewController, Boolean animated) [0x00021] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UINavigationController.g.cs:176 at AppMultiView.HomeScreen.m__2 (System.Object sender, System.EventArgs e) [0x00016] in /Users/Dan/Desktop/AppTouch_dev /Screens/HomeScreen.cs:75 at MonoTouch.UIKit.UIControlEventProxy.Activated () [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:30 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at AppMultiView.Application.Main (System.String[] args) [0x00000] in /Users/Dan/Desktop/AppTouch_dev /Main.cs:18

我是否试图反序列化错误的东西?

任何指点将不胜感激。

最佳答案

我不完全确定您所看到的问题是什么。可能只是你的 json 格式错误。

它有帮助,我通常解决这个问题的方法是:

  1. 使用类似 http://chris.photobooks.com/json/ 的工具验证 JSON - 在这种情况下,这向我揭示了一个错误 - 您的“ResponseData”数组未终止。固定代码为:

    {"ResponseData":[
        {"ClusterID":"c02f1f5c-c61b-4f2c-ab5a-249966b3cdef","ClusterName":"Northeast",
        "Courses":
        [
        {"CourseID":"8ab4f2b3-8160-4d7e-b79f-8d8b58926cc0","CourseName":"Home Course","SubCourses":
            [{"SubCourseName":"SubCourse1","SubCourseNumber":18}]},
        {"CourseID":"b3223464-333b-4c54-89c2-23908e0510c9","CourseName":"Away Course","SubCourses":
            [{"SubCourseName":"SubCourse1","SubCourseNumber":19}]}
        ],
        "IsHomeCluster":true}
    ]
    ,"ResponseErrors":[]}
    
  2. 将更正后的 JSON 复制到 http://json2csharp.com/

    这给了我这样的类(class):

    public class SubCours
    {
        public string SubCourseName { get; set; }
        public int SubCourseNumber { get; set; }
    }
    
    public class Cours
    {
        public string CourseID { get; set; }
        public string CourseName { get; set; }
        public List<SubCours> SubCourses { get; set; }
    }
    
    public class ResponseData
    {
        public string ClusterID { get; set; }
        public string ClusterName { get; set; }
        public List<Cours> Courses { get; set; }
        public bool IsHomeCluster { get; set; }
    }
    
    public class RootObject
    {
        public List<ResponseData> ResponseData { get; set; }
        public List<object> ResponseErrors { get; set; }
    }
    
  3. 使用JsonConvert.DeserializeObject<RootObject>(json)获取反序列化的 RootObject


还有一个新的 JSON 粘贴为类功能可用: http://blogs.msdn.com/b/webdev/archive/2012/12/18/paste-json-as-classes-in-asp-net-and-web-tools-2012-2-rc.aspx

关于json - 使用 Newtonsoft JSON 反序列化的 System.TypeLoadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14691840/

相关文章:

c# - 将序列化的 C# DateTime 转换为 JS Date 对象

php - 从jquery序列化函数发送值获取php数组

asp.net - 如何获取 JSON 字符串化数据

javascript - 在禁用 ng 的情况下搜索 JSON 对象(AngularJS)

firebase - Kotlin - 可序列化类中的初始化 block 只能读取默认属性值

sqlite - Xamarin PCL和sqlite.net

iphone - 用完 0 型蹦床

javascript - 如何使用 jQuery/Javascript 迭代表单字段

java - 400 在 Spring MVC 4 Rest 中转换 JSON 时出错

c++ - 混合 monotuch 和 Obj-C 或 C++