2

我真的被难住了。这是我返回的 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] 在 Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.在 Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in :0 中键入 objectType) [0x00000] 在 Newtonsoft.Json.JsonConvert.DeserializeObject (System.String 值, System.Type 类型,Newtonsoft.Json.JsonSerializerSettings 设置)[0x00000] 在:0 在 Newtonsoft.Json.JsonConvert.DeserializeObject(System.String 值)[0x00000] 在:0 在 AppMultiView.CourseInformationScreen。
在 /Users/Dan/Desktop/AppTouch_dev /Main.cs:18 中的 AppMultiView.Application.Main (System.String[] args) [0x00000]

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

任何指针将不胜感激。

4

1 回答 1

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


还有一个用于 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

于 2013-02-04T18:03:05.377 回答