[英]Conditionally serialize object members
To serialize object to json we do as given below - 要将对象序列化为json,我们如下所示-
var json = new JavaScriptSerializer().Serialize(question);
then it returns given json data :- 然后返回给定的json数据:-
{"que_desc":"devQuestion","qtype":3,"number_of_answer":3,"answers":[{"answer":"answer1","Question":null},{"answer":"answer2","Question":null},{"answer":"answer3","Question":null}]}
but I want to ignore "Question" property and need data as given below- 但我想忽略“问题”属性,并需要以下数据-
{
"que_desc": "This is Question details",
"qtype" : "1",
"number_of_answer" : "3",
"answers": [{"answer": "A", "is_default": "true"}, {"answer": "B"}, {"answer": "C"}]}
I want to ignore "Question" property while converting into json. 我想在转换为json时忽略“问题”属性。 so how we will conditionally serialize object members at run time??
那么我们如何在运行时有条件地序列化对象成员呢?
You could decorate the Question
property with the [ScriptIgnore]
attribute. 您可以使用
[ScriptIgnore]
属性装饰Question
属性。
For further info, please have a look here . 有关更多信息,请在这里查看 。
Supposing that Answer
has a definition like the below: 假设
Answer
具有如下定义:
public class Answer
{
public string Answer { get; set; }
public Question Question { get; set; }
// rest
}
If you change it to the following: 如果将其更改为以下内容:
public class Answer
{
public string Answer { get; set; }
[ScriptIgnore]
public Question Question { get; set; }
// rest
}
You will get that you want. 您会得到想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.