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

时间:2013-02-04 17:15:17

标签: 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:发生了类型加载异常。在   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值,   System.Type类型,Newtonsoft.Json.JsonSerializerSettings设置)   [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(包装器托管到原生)   MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_bool   (intptr,intptr,intptr,bool)at   MonoTouch.UIKit.UINavigationController.PushViewController   (MonoTouch.UIKit.UIViewController viewController,布尔动画)   [0x00021] in   /Developer/MonoTouch/Source/monotouch/src/UIKit/UINavigationController.g.cs:176   在AppMultiView.HomeScreen.m__2(System.Object sender,   System.EventArgs e)[0x00016]在/ 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   (包装器管理到本机)   MonoTouch.UIKit.UIApplication:UIApplicationMain   (int,string [],intptr,intptr)在MonoTouch.UIKit.UIApplication.Main   (System.String [] args,System.String principalClassName,System.String   delegateClassName)[0x0004c] in   /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  在AppMultiView.Application.Main(System.String [] args)[0x00000]中   / Users / Dan / Desktop / AppTouch_dev /Main.cs:18

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

任何指针都将非常感激。

1 个答案:

答案 0 :(得分:2)

我不完全确定你看到的问题是什么。可能只是你的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/

    这给了我这样的课程:

    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


  4. 还有一个新的适用于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