2016-03-20 361 views
0

我目前正在處理向我的控制器發送JSON數據的問題。JSON.stringify忽略嵌套對象

我發現當傳遞一個包含嵌套對象的對象時,嵌套對象將爲空。我想不通,我錯過了什麼?

我的服務器端是這樣的:

[HttpPost] 
public ActionResult ApplyChanges(List<Change> pChanges) 
{ 
    //the Issue occurs here in every object of pChanges: 
    //IgnoreFlag was populated correctly, but the Detection Object is null...? 
} 

public class Change 
{ 
    public Detection Detection { get; set; } 
    public bool IgnoreFlag { get; set; } 
} 

我的客戶方看起來是這樣的:

var data = [ 
    { 
    "Detection": { 
    "PropertyOld": 1, 
    "PropertyNew": 2, 
    }, 
    "IgnoreFlag": true 
    }, 
    { 
    "Detection": { 
    "PropertyOld": 3, 
    "PropertyNew": 4, 
    }, 
    "IgnoreFlag": false 
    } 
] 

$.ajax({ 
    type: "POST", 
    url: "/Url/To/ApplyChanges", 
    data: JSON.stringify({"pChanges": data}), 
    contentType: "application/json", 
    success: function (data, textStatus, jqXHR) { 
    //do something here 
}); 

有人可以幫我解決這個問題?

+0

儘量不字符串化,只是通過,因爲它是 –

+0

感謝您的答覆。我試過了,但它不起作用。相同的結果.. – Alan

+0

當我使用'JSON.stringify'對象時,不會忽略任何內容。 https://jsfiddle.net/qz3vp6gx/ –

回答

1

使用

JSON.stringify(data) 

而是採用

JSON.stringify({"pChanges": data})