To serialize object to json we do as given below -
var json = new JavaScriptSerializer().Serialize(question);
then it returns given json data :-
{"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. so how we will conditionally serialize object members at run time??
You can use the Json.NET
nuget and the [JsonIgnore]
atribute at the que_desc
property.
If you need more functionality, you can implemente de serialize methods by your self using Json.NET.
You could decorate the Question
property with the [ScriptIgnore]
attribute.
For further info, please have a look here .
Supposing that Answer
has a definition like the below:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.