要将对象序列化为json,我们按照以下给出 -
var json = new JavaScriptSerializer().Serialize(question);
然后返回给定的json数据: -
{"que_desc":"devQuestion","qtype":3,"number_of_answer":3,"answers":[{"answer":"answer1","Question":null},{"answer":"answer2","Question":null},{"answer":"answer3","Question":null}]}
但我想忽略"问题"属性和需求数据如下 -
{
"que_desc": "This is Question details",
"qtype" : "1",
"number_of_answer" : "3",
"answers": [{"answer": "A", "is_default": "true"}, {"answer": "B"}, {"answer": "C"}]}
我想忽略"问题"转换成json时的财产。 那么我们如何在运行时有条件地序列化对象成员?
答案 0 :(得分:1)
答案 1 :(得分:0)
您可以使用Question
属性装饰[ScriptIgnore]
属性。
有关详细信息,请查看here。
假设Answer
的定义如下:
public class Answer
{
public string Answer { get; set; }
public Question Question { get; set; }
// rest
}
如果您将其更改为以下内容:
public class Answer
{
public string Answer { get; set; }
[ScriptIgnore]
public Question Question { get; set; }
// rest
}
你会得到你想要的。