Model burayaJson nesnesi burada MVC
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; }
}
ve ben bu nesneyi alıyorum nasıl bu benim Json nesnesi
{
"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"
}
}
ve benim modelidir. "Değeri: Ben karşılık gelen model bunu Deserialize edebilmek JSONObject nesnesi elde edebilir ama değilim
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:);
i alıyorum Hata
akım JSON nesnesi serisini Can (örn { "adı" "}) türüne 'System.Collections.Generic.List`1 [Learning.Web.Models.CourseModel]' türü için ürününü düzgün şekilde serileştirmek üzere bir JSON dizisi (örneğin [1,2,3]) gerekiyor. Bu hatayı düzeltmek için JSON'u bir JSON dizisi olarak değiştirin (örn. [1,2,3]) veya normal bir .NET türü olacak şekilde seri hale getirilmiş türü değiştirin (örneğin, bir tamsayı değil tam sayı gibi değil) JSON nesnesinden serileştirilebilen bir dizi veya Liste gibi tipi. JsonObjectAttribute, bir JSON nesnesinden seri hale getirilmesi için 'u zorlamak üzere türe eklenebilir. Yol 'id', satır 1, pozisyon 6.
Ben WebAPI kullanıyorum ve ben burada ne yapıyorum yanlış emin değilim .. Herhangi bir yardım
Güncelleme takdir edilecektir:
Tabanlı Shyju ve Habib çalışır cevap i tek örneğine bu serisini ama eğer ben
string url = "http://localhost:8001/api/courses";
url değiştirmek ve onu Listeye serisini eğer ben aynı hatayı alıyorum, burada json nesnesi
olduğunu{
"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"
}
}
]
}
Tek örnek için çalışır .. ancak url dizesini url = "http: // localhost: 8001/api/courses" olarak değiştirirseniz; ve aynı hatayı aldım Liste'ye serilemek .. herhangi bir fikir? – Angloos
çünkü json verileriniz tek bir örnek için değil, bunların bir dizisidir. Parametreyi kullanmıyorsam – Shyju
.5 Farklı Json nesnesini alıyorum, diğer Json nesnesini de görüntülemem gerekiyor mu? – Angloos