내 Json 파일은 다음과 같습니다.
{
"blogPosts": [
{
"id": 1,
"date": "2019-11-11T18:11:22.511Z",
"title": "title1",
"image": "https://www.imageExample.com/static/ae8188adb9e0f13c40fce50bd773bc51/a6b7d/Content-considerations.jpg",
"htmlContent": "htmlExample",
"comments": [
{
"name": "Joe Bloggs",
"date": "2019-11-11T20:44:01.000Z",
"emailAddress": "[email protected]",
"message": "Sed vel odio consequat, elementum massa quis, lobortis est. Nulla egestas congue dolor, sit amet fermentum massa dignissim sit amet. In vestibulum iaculis egestas."
},
{
"name": "John Smith",
"date": "2019-11-13T09:00:23.533Z",
"emailAddress": "[email protected]",
"message": "Nam vel aliquet nulla, ac tempor ex. Suspendisse sit amet sollicitudin ex, vel placerat ipsum. Duis vitae fermentum eros. In maximus maximus purus, et volutpat eros rutrum et. Nulla fringilla at massa vel varius. In tristique egestas nisl, vitae elementum orci fringilla quis. Ut rutrum mauris erat, a rhoncus orci posuere non. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
},
{
"name": "Jack Black",
"date": "2019-11-11T19:22:22.511Z",
"emailAddress": "[email protected]",
"message": "Integer volutpat, sapien eu dapibus sodales, ipsum arcu dapibus elit, ac faucibus mi ligula suscipit mauris."
}
]
},
{
"id": 2,
"date": "2019-11-01T01:21:39.123Z",
"title": "title2",
"image": "https://www.imageExample.com/static/251940c537d045417ef75286eb970948/f9a6c/Ben.jpg",
"htmlContent": "htmlExample"
},
{
"id": 3,
"date": "2019-10-28T14:53:09.511Z",
"title": "title3",
"image": "https://www.imageExample.com/static/026bfc5011b0f64f2b912fd1d0ef87ae/f9a6c/brno.jpg",
"htmlContent": "htmlExample"
}
]
}
파일을 다음 클래스로 역 직렬화하려고합니다.
public class BlogRoot
{
[JsonProperty("blogPosts")]
public List<BlogPost> BlogPosts {get;set;}
}
public class Comment
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("date")]
public DateTime Date { get; set; }
[JsonProperty("emailAddress")]
public string EmailAddress { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
}
public class BlogPost
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("date")]
public DateTime Date { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("image")]
public Uri Image { get; set; }
[JsonProperty("htmlContent")]
public string HtmlContent { get; set; }
[JsonProperty("comments", NullValueHandling = NullValueHandling.Ignore)]
public List<Comment> Comments { get; set; }
public BlogPost(int id)
{
LoadBlogPost(id);
}
private List<BlogPost> LoadBlogPost(int id)
{
string path = HostingEnvironment.MapPath(@"~\App_Data\Blog-Posts - Copy.json");
string jsonFromFile;
using (StreamReader reader = File.OpenText(path))
{
jsonFromFile = reader.ReadToEnd();
var root = JsonConvert.DeserializeObject<BlogRoot>(jsonFromFile);
return root.BlogPosts;
}
}
}
Json 파일을 역 직렬화하려고하면 StackOverflowException이 발생하는데 그 이유를 모르겠습니다. 나는 온라인에서 어떤 해결책도 찾을 수 없으며 원인이 무엇인지 알아 내려고 머리를 찢고 있었는데, 아마 내가 완전히 간과 한 것이 분명한 것일 것이다. 재미있게도 Json을 동적 배열로 역 직렬화 할 수 있지만 이러한 클래스로 역 직렬화 할 수 있기를 원합니다. 어떤 아이디어?
콜 스택 :
> Web.dll!Web.Business.BlogPost.LoadBlogPost(int id) Line 39 C#
Web.dll!Web.Business.BlogPost.BlogPost(int id) Line 34 C#
[External Code]
Web.dll!Web.Business.BlogPost.LoadBlogPost(int id) Line 47 C#
Web.dll!Web.Business.BlogPost.BlogPost(int id) Line 34 C#
[External Code]
Web.dll!Web.Business.BlogPost.LoadBlogPost(int id) Line 47 C#
Web.dll!Web.Business.BlogPost.BlogPost(int id) Line 34 C#
[External Code]
Web.dll!Web.Business.BlogPost.LoadBlogPost(int id) Line 47 C#
Web.dll!Web.Business.BlogPost.BlogPost(int id) Line 34 C#
[External Code]
Web.dll!Web.Business.BlogPost.LoadBlogPost(int id) Line 47 C#
이보다 길지만이 줄을 반복합니다.
오류 메시지는 말 그대로 System.StackOverflowException이며 추가 세부 정보는 없습니다.
문제는 당신의 호출 LoadBlogPost()
클래스의 BlogPost
생성자에서 다음은이 List<BlogPost>
당신에 BlogRoot
. deseralization BlogPost
은 json에 새로운 foreach blogPost-Element 를 생성하려고 시도 하므로 생성자를 다시 호출하여 deserialization을 다시 호출합니다.
거기에 무한 루프가 있습니다 ;-)
대신 생성자에서 코드를 삭제하고 목록을 생성하는 정적 클래스 메서드를 만들 수 있습니다.
public BlogPost()
{
}
public static List<BlogPost> LoadBlogPost(int id)
{
string path = "my.xml";
string jsonFromFile;
using (StreamReader reader = File.OpenText(path))
{
jsonFromFile = reader.ReadToEnd();
var root = JsonConvert.DeserializeObject<BlogRoot>(jsonFromFile);
return root.BlogPosts;
}
}
그런 다음 다음과 같이 호출하십시오.
var blogPosts = BlogPost.LoadBlogPost(0);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다