2014-04-19 11 views
4

내가 가지고있는 다음과 같은 데이터 :JavascriptSerializer 예외

{ "데이터": { "ID": "7IaWnXo", "제목"널 (null), "설명"널 (null), "날짜"1397926970 " false ","width ": 60,"height ": 60,"size ": 1277,"views ": 0,"bandwidth ": 0,"favorite ": false , "nsfw": null, "section": null, "deletehash": "KYIfVnHIWWTPifh", "link": "http://i.imgur.com/7IaWnXo.png"}, "success": true, "status ": 200}

나는 이것으로 그것을 시리얼 라이저 시도하고있다 :

public struct ImageInfoContainer 
    { 
     public ImageInfo data {get; set;} 
     bool success { get; set; } 
     string status { get; set; } 
    } 
    public struct ImageInfo 
    { 
     public string id {get; set;} 
     public string title { get; set; } 
     public string url { get; set; } 
     public string description {get; set;} 
     public string datetime {get; set;} 
     public string type {get; set;} 
     public string animated {get; set;} 
     public int width {get; set;} 
     public int height {get; set;} 
     public int size {get; set;} 
     public int views {get; set;} 
     public int bandwidth {get; set;} 
     public bool favourite {get; set;} 
     public bool nsfw {get; set;} 
     public string section {get; set;} 
     public string deletehash {get; set;} 
     public string link {get; set;} 
    } 

내가 받고 있어요 :

'System.InvalidOperationException'System.Web.Extensions.dll에서 발생했지만 사용자 코드에서 처리되지 않은 유형의 예외

추가 정보 : 널 (null)를 변환 할 수 없습니다 값 형식으로.

내가 뭘 잘못하고 있니?

+0

''nsfw ': null'은'true' 또는'false'이어야합니다. 'section'은 null이 아니어야합니다. – Ofiris

답변

10

JSON 데이터에 : nsfw이 null입니다.
그러나 ImageInfo 구조체에, nsfwboolean로 정의되어

당신은이 가능성을 가지고 (그것은 true 또는 false, null이 될 수 없습니다 수 있습니다.) 당신이 JSON 데이터에 액세스 할 수있는 경우

  • , nsfw에 대한 null을 허용하지 않습니다.
  • 가 널 (NULL) BOOL 사용 : 당신은 두 번째 옵션을 가지고가는 경우에 public bool? nsfw {get; set;}

을, 이것은 당신이 nsfw의 값으로 true, false 또는 null을 할 수 있습니다 그리고 당신은 더 이상이 오류가 없습니다.

Nullable<bool>bool?은 모두 같은 구문입니다.
Nullable Types