我有一些json,我将其传递给具有Guid作为属性名称的C#方法,如下所示
function sendData() {
var Mydata = {
"answers": {
"3fe69e7e-d753-4079-963e-e4c7dd8643ea": "some name",
"084cb0ad-040c-49e5-bc4f-3d369b50cbef": "An alternative therapist",
"084cb0ad-040c-49e5-bc4f-3d369b50cbef-additional": "n/a",
"77481c26-796f-4f95-b406-e0aa81c54c09": "No",
"77481c26-796f-4f95-b406-e0aa81c54c09-additional": "n/a",
"d2fe04bd-70a5-475e-a908-24e79e449502": "Infection of the urinary system, UTI"
}
};
$.ajax({
data: JSON.stringify(Mydata),
dataType: 'json',
type: 'post',
contentType: 'application/json',
url: "xxxxxxxxxxxxxxxxx/PostData"
});
}
我正在尝试将属性映射到我已定义的C#对象,如
public class Answers{
[JsonProperty("3fe69e7e-d753-4079-963e-e4c7dd8643ea")]
public string Name { get; set; }
}
当前,我只是试图将值写到控制台,但是name属性始终为null。
[HttpPost]
public void PostData(Answers answers) {
Console.WriteLine(answers.Name);
}
在Ajax调用中作为参数传递时,能否使C#根据jsonproperty属性将值自动映射到正确的属性?
因此,在进一步研究中似乎忽略了jsonproperty属性。有人有什么想法吗?
答案 0 :(得分:0)
对于要传递的Json,可能需要如下所示的DTO结构。
public class RootObject
{
public Answers answers { get; set; }
}
public class Answers{
[JsonProperty("3fe69e7e-d753-4079-963e-e4c7dd8643ea")]
public string Name { get; set; }
}