经过一番争论,我终于让我的 Json 结果在我的 web api 中为具有关联的类正常工作:
public string GetAll()
{
var order =_repository.GetAll();
var settings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
return JsonConvert.SerializeObject(order, settings);
}
但是当我在浏览器或 Fiddler 中进行测试时,而不是典型的类似:
(一种)
"Books":
[
{
"Id": "1",
"Name": "Book1",
"Authors":
[
{
.......
}
]
}
]
(B) 我明白了:
"[{\"Id\":1,\"Name\":\"Book1\",\"Authors\":[{\"Id\":1,\"PersonId\":1,\"Person \":null,\"Books\":[{\"Id\":3,\"Name\":\"Book3\",\"Authors\":[{\"Id\":4,\ "PersonId\":4,\"Person\":null,\"Books\":[{\"Id\":2,\"Name\":\"Book2\",\"Authors\":[ {\"Id\":2,\"PersonId\":2,\"Person\":null,\"Books\": .....
有人可以告诉我我必须做什么才能像上面的 A. 那样格式化它吗?
谢谢
更新:
顺便说一句,Formatting.Indented 使情况变得更糟:
"[\r\n {\r\n \"Id\": 1,\r\n \"Name\": \"Book1\",\r\n \"Authors\": [\r\n {\r\n \"Id\": 1,\r\n \"PersonId\": 1,\r\n \"Person\": {\r\n \"Id\": 1,\r \n \"FirstName\": \"John\",\r\n \"LastName\": \"Doe\"\r\n },\r\n \"Books\": [\r\n {\r\n \"Id\": 3,\r\n \"Name\": \"Book3\",\r\n \"Authors\": [\r\n {\r\n \ "Id\": 4,\r\n \"PersonId\": 4,\r\n \"Person\": {\r\n \"Id\": 4,\r\n \"FirstName\ ":\"朱莉\",\r\n