2
votes

I am truly stumped here. Here is my JSON returned:

{"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":[]}

This is my code to deserialize:

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

Project compiles fine, but when I run it in the simulator, I get this error:

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

Am I trying to deserialize the wrong thing?

Any pointers will be greatly appreciated.

1

1 Answers

2
votes

I'm not entirely sure what the issue you are seeing is. It may just be that your json is badly formatted.

It it helps, the way I'd normally tackle this is to:

  1. Use a tool like http://chris.photobooks.com/json/ to validate the JSON - in this case, this revealed to me an error - your "ResponseData" array was not terminated. The fixed code is:

    {"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. copy the corrected JSON into http://json2csharp.com/

    This gives me classes like:

    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. Use the JsonConvert.DeserializeObject<RootObject>(json) to get a deserialized RootObject


There is also a new Paste As Classes feature for JSON available: http://blogs.msdn.com/b/webdev/archive/2012/12/18/paste-json-as-classes-in-asp-net-and-web-tools-2012-2-rc.aspx