2012-08-30 3 views
1

나는이 JSON을 역 직렬화하기 위해이 코드를하지만 난이 오류가 계속를 역 직렬화 어떻게 :나는이 JSON

"Cannot convert object of type System.String to type Namespace.Models.Url"

. json으로 구조는 다음과 같습니다 :

[ { fileUrl: "localhost/ContentManager/get/ovYWB0/81/wallpaper" }, { fileUrl: "localhost/ContentManager/get/AcjwO0/81/wallpaper" }, { fileUrl: "localhost/ContentManager/get/HCR0q0/81/wallpaper" } ]

내가 다음과 같습니다 매핑하고 클래스 : 현재 JSON 구조 따르면

var serializer = new JavaScriptSerializer(); 
IList<MTContribute> data = new List<MTContribute>(); 

var items = serializer.Deserialize<List<Response>>(json); 
foreach (var item in items) 
{ 
    var newData = new MTContribute 
    { 
     DateCreated = DateTime.Today, 
     IsActive = Convert.ToBoolean("True"), 
     MTContributeCategoryId = Category.MTContributeCategoryId, 
     Url = item.FileUrl.FileUrl 
    }; 

    data.Add(newData); 
} 

답변

0

:

public class Url 
{ 
    public string FileUrl { get; set; } 
} 

public class Response 
{ 
    public Url FileUrl { get; set; } 
} 

직렬화 복원 코드는 다음과 같습니다 이게 작동해야합니다.

var items = serializer.Deserialize<List<Url>>(json); // Url instead of Response 

이것이 작동하지 않는다면, Response 클래스의 구조를 반영하기 위해 json 구조를 변경해야합니다.

내가 뭔가를 놓친하지 않는 한이 필요한 JSON 구조

[{ fileUrl:{ fileUrl: "localhost/ContentManager/get/ovYWB0/81/wallpaper" }}] 
입니다