2012-12-04 4 views
1

에서 ASP.NET MVC 모델을 직렬화 이것은 내 컨트롤러내가 JSON으로 모델을 직렬화 할이</p> <pre><code>public class Category : BaseFieldsTables { public ICollection<Category> Categories { get; set; } public Category Parent { get; set; } public int? ParentId { get; set; } } </code></pre> <p>같은 모델을 가지고 JSON

var categories = 
      _efCategory.List().ToList(). 
       ToList(). 
       Select(x => new {id = x.Id, title = x.Name, children = x.Parent}); 

     string output = JsonConvert.SerializeObject(categories, Formatting.Indented, 
                new JsonSerializerSettings 
                 { 
                  PreserveReferencesHandling = PreserveReferencesHandling.Objects 
                 }); 

     return Json(output.Replace, JsonRequestBehavior.AllowGet); 

하지만 나는이 결과를 얻을 수

"[\r\n {\r\n \"$id\": \"1\",\r\n \"id\": 1,\r\n \"title\": \"News\",\r\n \"children\": null\r\n },\r\n {\r\n \"$id\": \"2\",\r\n \"id\": 2,\r\n \"title\": \"2012\",\r\n \"children\": 
{\r\n  \"$id\": \"3\",\r\n  \"Categories\": [\r\n  {\r\n   \"$id\": 

답변

0

이것은 정상입니다. 당신이보고있는 모든 것은 문자열의 이스케이프 된 버전입니다 (아마 Visual Studio에서 직접). \r\n은 줄 바꿈 문자와 동일하며 \""을 피할 수있는 방법입니다. 출력 됨, 정상적으로 표시되어야합니다.

+0

하지만 난 나무를 위해 이것을 사용하려면 내가 [ { "$ ID가"이 오류 catch되지 않은 잘못된 데이터 형식 얻을 : "1", "ID": 2, "제목": "뉴스", 을 "children": null }, –

+0

즉, JSON을 잘못 구문 분석하고 있음을 의미합니다. 더 많은 코드를 게시해야합니다 ... JSON 자체가 정확합니다. –

+0

이 "$ id": "13", "id": 12, –

관련 문제

 관련 문제