2016-04-07 119 views
2

這裏是我的JSON對象JSON對象到模型MVC

{ 
    "id":5, 
    "url":"http://localhost:8001/api/courses/5", 
    "name":"Professional Experience II", 
    "duration":"5.0", 
    "description":"The course will talk in depth about every thing", 
    "tutor": { 
      "id":2, 
      "email":"[email protected]", 
      "userName":"TA", 
      "firstName":"T", 
      "lastName":"A", 
      "gender":0 
      }, 
    "subject": 
      { 
      "id":2, 
      "name":"Science" 
      } 
    } 

,我的模型是在這裏

public class CourseModel 
    { 
     public int Id { get; set; } 
     public string Url { get; set; } 
     public string Name { get; set; } 
     public double Duration { get; set; } 
     public string Description { get; set; } 
     public TutorModel Tutor { get; set; } 
     public SubjectModel Subject { get; set; } 
    } 

和我這是怎麼得到這個對象。我能夠得到的JSONObject,但不能將它反序列化到其相應的模型

string url = "http://localhost:8001/api/courses/5"; 
HttpResponseMessage responseMessage = await client.GetAsync(url); 
if (responseMessage.IsSuccessStatusCode) 
{ 
    var responseData = responseMessage.Content.ReadAsStringAsync().Result;   
    var data = JsonConvert.DeserializeObject<List<CourseModel>>(responseData); 
    return View(data); 
} 
return View("Error:); 

錯誤我得到是

無法反序列化當前的JSON對象(例如{「名」:「值「}) 轉換爲 'System.Collections.Generic.List`1 [Learning.Web.Models.CourseModel]' ,因爲該類型需要JSON數組(例如[1,2,3])才能正確反序列化 。要解決這個錯誤,可以將JSON更改爲JSON數組(例如[1,2,3]),或者更改反序列化類型,使其成爲正常的 .NET類型(例如,不是像integer這樣的基本類型,也不是集合 類型類似於數組或列表),可以從JSON 對象反序列化。 JsonObjectAttribute也可以添加到類型中,以強制它從一個JSON對象反序列化。路徑「身份證」,1號線,6位

我使用的WebAPI,我不知道我在做什麼錯在這裏。任何幫助將不勝感激

更新時間:

基礎的在Shyju和哈比卜回答它的工作,如果我把它反序列化到一個單一的實例,但如果我的網址更改爲

string url = "http://localhost:8001/api/courses"; 

和反序列化到名單,我得到同樣的錯誤,這裏是JSON對象

{ 
     "totalCount":32, 
     "totalPages":4, 
     "prevPageLink":"", 
     "nextPageLink":"http://localhost:8001/api/courses?page=1&pageSize=10", 
     "results":[ 
      { 
      "id":1, 
      "url":"http://localhost:8001/api/courses/1", 
      "name":"History Teaching Methods 1", 
      "duration":3.0, 
      "description":"The course will talk in depth about: History Teaching Methods 1", 
      "tutor":{ 
       "id":1, 
       "email":"[email protected]", 
       "userName":"A, 
       "firstName":"A", 
       "lastName":"J", 
       "gender":0 
      }, 
      "subject":{ 
       "id":1, 
       "name":"History" 
      } 
      }, 
      { 
      "id":2, 
      "url":"http://localhost:8001/api/courses/2", 
      "name":"History Teaching Methods 2", 
      "duration":5.0, 
      "description":"The course will talk in depth about: History Teaching Methods 2", 
      "tutor":{ 
       "id":1, 
       "email":"[email protected]", 
       "userName":"AJo", 
       "firstName":"A", 
       "lastName":"Jo", 
       "gender":0 
      }, 
      "subject":{ 
       "id":1, 
       "name":"History" 
      } 
      }, 
      { 
      "id":3, 
      "url":"http://localhost:8001/api/courses/3", 
      "name":"History Teaching Methods 3", 
      "duration":5.0, 
      "description":"The course will talk in depth about: History Teaching Methods 3", 
      "tutor":{ 
       "id":1, 
       "email":"[email protected]", 
       "userName":"A", 
       "firstName":"A", 
       "lastName":"J", 
       "gender":0 
      }, 
      "subject":{ 
       "id":1, 
       "name":"History" 
      } 
      }, 
      { 
      "id":4, 
      "url":"http://localhost:8001/api/courses/4", 
      "name":"Professional Experience 1 (Mathematics/Science)", 
      "duration":5.0, 
      "description":"The course will talk in depth about: Professional Experience 1 (Mathematics/Science)", 
      "tutor":{ 
       "id":2, 
       "email":"[email protected]", 
       "userName":"T", 
       "firstName":"T", 
       "lastName":"A", 
       "gender":0 
      }, 
      "subject":{ 
       "id":2, 
       "name":"Science" 
      } 
      }, 
      { 
      "id":5, 
      "url":"http://localhost:8001/api/courses/5", 
      "name":"Professional Experience 2 (Mathematics/Science)", 
      "duration":5.0, 
      "description":"The course will talk in depth about: Professional Experience 2 (Mathematics/Science)", 
      "tutor":{ 
       "id":2, 
       "email":"[email protected]", 
       "userName":"T", 
       "firstName":"T", 
       "lastName":"A", 
       "gender":0 
      }, 
      "subject":{ 
       "id":2, 
       "name":"Science" 
      } 
      }, 
      { 
      "id":6, 
      "url":"http://localhost:8001/api/courses/6", 
      "name":"Professional Experience 3 (Mathematics/Science)", 
      "duration":5.0, 
      "description":"The course will talk in depth about: Professional Experience 3 (Mathematics/Science)", 
      "tutor":{ 
       "id":2, 
       "email":"[email protected]", 
       "userName":"T", 
       "firstName":"T", 
       "lastName":"A", 
       "gender":0 
      }, 
      "subject":{ 
       "id":2, 
       "name":"Science" 
      } 
      }, 
      { 
      "id":7, 
      "url":"http://localhost:8001/api/courses/7", 
      "name":"Geography Teaching Methods 1", 
      "duration":5.0, 
      "description":"The course will talk in depth about: Geography Teaching Methods 1", 
      "tutor":{ 
       "id":3, 
       "email":"[email protected]", 
       "userName":"Ti", 
       "firstName":"T", 
       "lastName":"W", 
       "gender":0 
      }, 
      "subject":{ 
       "id":3, 
       "name":"Geography" 
      } 
      }, 
      { 
      "id":8, 
      "url":"http://localhost:8001/api/courses/8", 
      "name":"Geography Teaching Methods 2", 
      "duration":5.0, 
      "description":"The course will talk in depth about: Geography Teaching Methods 2", 
      "tutor":{ 
       "id":3, 
       "email":"[email protected]", 
       "userName":"f", 
       "firstName":"f", 
       "lastName":"f", 
       "gender":0 
      }, 
      "subject":{ 
       "id":3, 
       "name":"Geography" 
      } 
      }, 
      { 
      "id":9, 
      "url":"http://localhost:8001/api/courses/9", 
      "name":"Geography Teaching Methods 3", 
      "duration":5.0, 
      "description":"The course will talk in depth about: Geography Teaching Methods 3", 
      "tutor":{ 
       "id":3, 
       "email":"[email protected]", 
       "userName":"s", 
       "firstName":"s", 
       "lastName":"s", 
       "gender":0 
      }, 
      "subject":{ 
       "id":3, 
       "name":"Geography" 
      } 
      }, 
      { 
      "id":10, 
      "url":"http://localhost:8001/api/courses/10", 
      "name":"English Education 1", 
      "duration":5.0, 
      "description":"The course will talk in depth about: English Education 1", 
      "tutor":{ 
       "id":4, 
       "email":"[email protected]", 
       "userName":"g", 
       "firstName":"g", 
       "lastName":"g", 
       "gender":0 
      }, 
      "subject":{ 
       "id":4, 
       "name":"English" 
      } 
      } 
     ] 
    } 

回答

3

您的JSON代表CourseModel的單個實例。您應該將其反序列化爲CourseModel的單個實例,而不是這些實例的列表。

這應該工作。

var data = JsonConvert.DeserializeObject<CourseModel>(responseData); 
+0

它可以爲單個實例工作..但如果我更改url爲字符串url =「http:// localhost:8001/api/courses」;並反序列化它列表我得到同樣的錯誤..任何想法? – Angloos

+0

,因爲你的json數據是針對單個實例的,而不是它們的數組。如果我不使用參數即 – Shyju

+0

5我得到不同的Json對象,我是否還需要顯示另一個Json對象? – Angloos

0

我相信你已經設置你的TutorSubject類正確的錯誤是因爲你嘗試反序列化一個對象到List<T>

DO:

var data = JsonConvert.DeserializeObject<CourseModel>(responseData); 

而且到創建相關模型,您可以將您的JSON粘貼到http://json2csharp.com/並獲取生成的模型,如下所示:

public class Tutor 
{ 
    public int id { get; set; } 
    public string email { get; set; } 
    public string userName { get; set; } 
    public string firstName { get; set; } 
    public string lastName { get; set; } 
    public int gender { get; set; } 
} 

public class Subject 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
} 

public class CourseModel 
{ 
    public int id { get; set; } 
    public string url { get; set; } 
    public string name { get; set; } 
    public string duration { get; set; } 
    public string description { get; set; } 
    public Tutor tutor { get; set; } 
    public Subject subject { get; set; } 
}