2011-10-12 6 views
0

Silverlight 및 .NET 아키텍처를 처음 사용합니다. Windows Phone 7 프로젝트에서 작업하고 있습니다. 서버에서 일부 JSON 형식 데이터를 수신합니다. WebClient 인터페이스 콜백에서 서버로부터 데이터를받습니다. 그러나 C# 개체의 데이터를 serialize 할 수 없습니다. 나는 다음 코드를 사용하고DeSerializing complex json wp7

public void GetData_Completed(object sender, DownloadStringCompletedEventArgs e) 
    { 

     byte[] encodedString = Encoding.UTF8.GetBytes(e.Result); 

     //// Put the byte array into a stream and rewind it to the beginning 
     MemoryStream ms = new MemoryStream(encodedString); 
     ms.Flush(); 
     ms.Position = 0; 

     // convert json result to model 
     Stream stream = ms; 
     DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(SchedulesResults)); 
     SchedulesResults myResults = 
     (SchedulesResults)dataContractJsonSerializer.ReadObject(stream); 

     var result = myResults; 
    } 

내가 얻을 가정하고 데이터 형식이

schedules: [ 
    { 
     id: 2897 
     progress: -9 
     state: complete 
     starts_at: 1315267800 
     primary_topic: { 
      id: 13 
     } 
     secondary_topic: { 
      id: 9 
     } 
     scores: [ 
      { 
      schedule_id: 2897 
      score: 0 
      topic_id: 13 
      } 
      { 
      schedule_id: 2897 
      score: 4 
      topic_id: 9 
      } 
     ] 
    } 
    . 
    . 
    . 

처럼이 내가 serilaize에게

public class SchedulesResults 
{ 

    /// <summary> 
    /// Gets or sets the results. 
    /// </summary> 
    /// <value>The results.</value> 
    public Schedules[] results { get; set; } 
} 

public class Schedules 
{ 
    int id { get; set; } 
    int progress { get; set; } 
    string state { get; set; } 
    DateTime starts_at { get; set; } 
    primary_topic primary_topic_ID { get; set; } 
    secondry_topic secondary_topic_ID { get; set; } 
    Scores[] scores { get; set; } 

} 
public class primary_topic 
{ 
    int id { get; set; } 
} 

public class secondry_topic 
{ 
    int id { get; set; } 
} 

public class Scores 
{   
     int schedule_id{ get; set; } 
     int score { get; set; } 
     int topic_id { get; set; } 

} 
을 해제하기 위해 사용하고있는 클래스입니다

하지만 내가 값에 null이 점점 직렬화에. 어디에서 잘못 될지 말해주세요.

데이터의 종류는 내가 서버에서 얻고있다 당신은 그것이해야처럼 보인다 (사용할 수있는 코드에서), 시도하고 MyCLASS의 인스턴스로 응답을 역 직렬화하지만 것처럼 보이는 것

{"schedules":[{"id":3499,"progress":-9,"state":"complete","starts_at":1317945600,"primary_topic":{"id":6},"secondary_topic":{"id":11},"scores":[{"schedule_id":3499,"score":2,"topic_id":6},{"schedule_id":3499,"score":3,"topic_id":11}]}, 
+1

이 생성 도움이 될 마시고의 : HTTP : //json2csharp.com/ –

+0

덕분에 많은 데릭 ... 그것은 효과가 있었다. – Avijeet

답변

1

내게는 ScheduleResults의 속성이 schedules이 아니고 results이 아니어야합니다.


나는이 의미 :

public class SchedulesResults 
{ 

    /// <summary> 
    /// Gets or sets the results. 
    /// </summary> 
    /// <value>The results.</value> 
    public Schedules[] results { get; set; } 
} 

이를해야합니다

public class SchedulesResults 
{ 

    /// <summary> 
    /// Gets or sets the results. 
    /// </summary> 
    /// <value>The results.</value> 
    public Schedules[] schedules { get; set; } 
} 
+0

안녕하세요, 저는 "보고서"가 의미하는 바를 얻지 못하고 있습니다. – Avijeet

+0

또한 자세한 정보를 제공합니다. 이것은 서버 { "일정": 3499, "진행": 9, "상태": "완료", "starts_at": 1317945600, "primary_topic": { "id": 6}, "secondary_topic": { "id": 11}, "scores": [{ "schedule_id": 3499, "score": 2, "topic_id": 6} , { "schedule_id": 3499, "score": 3, "topic_id": 11}}}, – Avijeet

+0

@avijeet : 나는 의미하는 바를 예를 들어 업데이트했습니다. – cjk

0

유형은 SchedulesResults입니다.

+0

내 사과는 복사 붙여 넣기 실수였습니다. 나는 실제로 Scheduleresults를 사용하고 있습니다. 올바른 클래스 구조를 만들면 내 관심사가 무엇입니까? – Avijeet