首先请从图片开始我已经附加了json输出视图。在图片上你会看到我只能为根级别对象分配值,但在子级别上我无法正确分配我需要的值。我的主要目标是获得与Json代码完全相同的输出。如何修复我的控制器以获得JavaScriptSerializer的示例json类型输出?
控制器:
var root = new RootObject();
root.name = "Jan Kowalski";
root.email = "jan.kowalski@wp.pl";
root.dayOfCycle = "10";
//root.tags.Add(new Tag
//{
// tagId = "Xw",
//});
//root.scoring = 25;
//root.customFieldValues.Add(new CustomFieldValue
//{
// customFieldId = "n",
// //value = "white", value should be list
//});
root.ipAddress = "89.206.36.193";
var camp = new Campaign();
camp.campaignId = "jf7e3jn";
var jsBody = new JavaScriptSerializer().Serialize(root);
模特课:
public class Campaign
{
public string campaignId { get; set; }
}
public class Tag
{
public string tagId { get; set; }
}
public class CustomFieldValue
{
public string customFieldId { get; set; }
public List<string> value { get; set; }
}
public class RootObject
{
public string name { get; set; }
public string email { get; set; }
public string dayOfCycle { get; set; }
public Campaign campaign { get; set; }
public List<Tag> tags { get; set; }
public int scoring { get; set; }
public List<CustomFieldValue> customFieldValues { get; set; }
public string ipAddress { get; set; }
}
JSON身体我想从JavaScriptSerializer:
{
"name": "Jan Kowalski",
"email": "jan.kowalski@wp.pl",
"dayOfCycle": "10",
"campaign": {
"campaignId": "jf7e3jn"
},
"tags": [
{
"tagId": "Xw"
},
{
"tagId": "Nn"
}
],
"scoring": 25,
"customFieldValues": [
{
"customFieldId": "n",
"value": [
"white"
]
}
],
"ipAddress": "89.206.36.193"
}
答案 0 :(得分:0)
已经评论过,为了完整起见:
root.campaign = camp;
jsBody
root.tags = new List<Tag>();
之前缺少root.tags.Add
以防止出现空引用异常