-1

JSON을 C#에서 deserialize하려고하는데 NullReferenceException이 발생하고 이유를 모르겠습니다. 여기 JSON을 C#에서 Deserialzing

는 JSON 내가 구문 분석을 시도하고있다 :

{"Entries": {"Entry": {"day": "28","month": "10","year": "1955","type": "birthday","title": "Bill Gates was born!","picture": "","video": ""}}} 

을하고이 코드

public class Entry 
{ 
    public string day { get; set; } 
    public string month { get; set; } 
    public string year { get; set; } 
    public string type { get; set; } 
    public string title { get; set; } 
    public string picture { get; set; } 
    public string video { get; set; } 
} 

public class Entries 
{ 
    public List<Entry> entry { get; set; } 
} 

private void buttonSearch_Click(object sender, EventArgs e) 
{ 
    string json = new StreamReader("events.json").ReadToEnd(); 
    var entries = JsonConvert.DeserializeObject<Entries>(json); 
    MessageBox.Show(entries.entry[0].day); // NullReferenceException 
} 

왜이 오류가 무엇입니까 나는 그것을 어떻게 해결할 수를 사용하고 있습니다?

내가

{"Entries": ["Entry": {"day": "28","month": "10","year": "1955","type": "birthday","title": "Bill Gates was born!","picture": "","video": ""}]} 

에 JSON을 변경할 때 얻을 After parsing a value an unexpected character was encountered: :. Path 'Entries[0]', line 1, position 20.

내가 JSON과 하나의 울부 짖는 소리와 함께 연주

편집이 나를 위해 일한 :

[{"day": "28","month": "10","year": "1955","type": "birthday","title": "Bill Gates was born!","picture": "","video": ""}] 
+0

디버거를 연결합니다. 실제 비 직렬화 된 데이터를 검사하십시오. 어쨌든 문제는 * JSON에 배열이 없다는 것입니다. – user2246674

+0

디버거는 항목이 null이며 그 이유를 알 수 없다고 말합니다. – armin

+0

".. 문제는 * JSON에 배열이 없습니다"입니다. 디시리얼라이저는'List entry'를 채울 수있는 배열이 필요합니다. 디시리얼라이저 신뢰 - 필드를 올바르게 채우지 않으면 소스 데이터가 정렬되지 않은 것일 수 있습니다. 또한 일부 이름 (예 : 항목 대 항목)이 올바르지 않습니다. – user2246674

답변

1

귀하의 json은 공동입니다. 다음과 같이 클래스 정의를 변경해야하는 경우 rrect, 다음이

(BTW : 당신이 유용 this site을 찾을 수 있습니다) 작동합니다

var entries = JsonConvert.DeserializeObject<Root>(json); 

public class Entry 
{ 
    public string day { get; set; } 
    public string month { get; set; } 
    public string year { get; set; } 
    public string type { get; set; } 
    public string title { get; set; } 
    public string picture { get; set; } 
    public string video { get; set; } 
} 

public class Entries 
{ 
    public Entry Entry { get; set; } 
} 

public class Root 
{ 
    public Entries Entries { get; set; } 
} 
+0

+1 사이트 링크! 그걸 몰랐어. –

+0

답장을 보내 주셔서 감사합니다. 그러나 저에게 도움이되지 못했습니다. 위의 녀석이 제 JSON이 잘못되었다고 말했습니다 (업데이트 된 버전 확인). 클래스 항목이 클래스 항목의 배열을 가질 필요가 있습니다. – armin