2014-12-10 3 views
0

코스의 데이터를 직렬화하려고합니다. 나는 코스 ID (C_id)와 코스 이름 (C_Name)을 얻는다. 나는 학생의 반발이라는 또 하나의 것을 얻고있다. 이것을 직렬화하려고하면 코스에 등록한 학생의 중첩 목록을 가져올 수 없습니다. 위의 코드에서 중첩 된 객체 직렬화 (목록)

var u = (from g in t.courses 
     select g) 
     .ToList(); 

List<course> ui = u 
    .Select(d => new course() 
     { C_Name = d.C_Name, 
      C_Id = d.C_Id, 
      student = d.student 
     }) 
    .ToList(); 

ASCIIEncoding objASCIIEncoding = new ASCIIEncoding(); 

string strData = JsonConvert 
    .SerializeObject(ui, Formatting.Indented, new JsonSerializerSettings() 
{ 
    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore 
}); 

나는 중첩으로 students 목록을 적절한 데이터를 얻고 있지만,이 라인

string strData = JsonConvert.SerializeObject(ui, Formatting.Indented, new JsonSerializerSettings() 
{ 
    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore 
}); 

가 실행될 때 난 단지 코스 ID (C_ID) 물론 이름 (C_Name)을 얻는다. 중첩 된 student 목록은 직렬화되지 않습니다. 문서에서

+0

참조 루프 처리 및 무한 중첩이있는 데이터와 관련이있는 것 같습니다. 자세한 내용은 http://stackoverflow.com/questions/11979637/what-does-referenceloophandling-ignore-in-newtonsoft-json-exactly-do를 참조하십시오. – link64

답변

0

: Json.NET가 참조 루프에서 개체를 무시하지 에게 그러한 직렬화합니다

. 객체가 처음 발견되면 보통 으로 직렬화되지만 객체가 자체 객체의 자식 객체로 발생하면 직렬자가 직렬화를 건너 뜁니다.

http://james.newtonking.com/json/help/index.html?topic=html/SerializationSettings.htm

나는 당신의 학생들이 교육 과정에 대한 참조를 포함 같은데?

student = d.student.Select(s => new {s.studentId, s.studentName}).ToList() 
0

는 희망이가 PreserveReferencesHandling 모든 객체 직렬화 방법을 변경합니다 JsonSerializer에 설정 http://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm

을하는 데 도움이 : 아마도 당신은 과정에 대한 참조를 제거하려면 다음과 유사한 일을 수행하여 그들이 해달라고 확인해야 및 deserialized. 개체 및 멤버를 참조로 serialize해야하는 미세 입자 제어의 경우 JsonObjectAttribute, JsonArrayAttribute 및 JsonPropertyAttribute에 IsReference 속성이 있습니다. JsonObjectAttribute 또는 JsonArrayAttribute에서 IsReference를 true로 설정하면 JsonSerializer가 항상 특성을 참조로 사용하는 형식을 serialize합니다. JsonPropertyAttribute의 IsReference를 true로 설정하면 해당 속성 만 참조로 serialize됩니다.

[JsonObject(IsReference = true)] 
public class EmployeeReference 
{ 
    public string Name { get; set; } 
    public EmployeeReference Manager { get; set; } 
}