0

have class whose structure is like

class QuestionNode
{
    public String Value { get; set; }
    public int? QId { get; set; }
    public String QuesDiscription { get; set; }
    public String QuesType { get; set; }
    public String Editable { get; set; }
    public int? PrevQues { get; set; }
    public String UserResponse { get; set; }
    public int UserSelected { get; set; }
    public int Progress { get; set; }

    public List<QuestionNode> OptionSet { get; set; }
    public QuestionNode(int? QId,int? PreQues, String Value){}
    public QuestionNode(int? PreQues, String Value){}
    public QuestionNode (String Value,int? QId,string QuesDiscription,string QuesType ,string Editable ,int?  PrevQues,int? NextQues,int Progress ){}
    public QuestionNode(String Value, int? QId, string QuesDiscription, string QuesType, string Editable, int? PrevQues, int UseResponse, int UserSelected, int Progress, List<QuestionNode> OptionSet){}

}

I am able to convert this structure into json using NewtonSoft.Json. but while Deserializeing it using

Newtonsoft.Json.JsonConvert.DeserializeObject<QuestionNode>(json);

I am getting this exception:

Unable to find a constructor to use for type TestingJson.QuestionNode.
A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Ques', line 2, position 10.

3
  • Try commenting last line public QuestionNode(String Value, int? QId, string QuesDiscription, string QuesType, string Editable, int? PrevQues, int UseResponse, int UserSelected, int Progress, List<QuestionNode> OptionSet){} and check if it works Commented Nov 23, 2015 at 6:37
  • Please add the JSON content to your question, because creating and filling and serializing an instance of this is not easy. The error message says it all by the way, The problem is probably not the class containing a reference to itself, it is with it's constructors. Please add full source code of QuestionNode class Commented Nov 23, 2015 at 6:38
  • I am creating a tree like structure with QuestionNode . After creating the structure I am able to create a json string using Newtonsoft.Json .I am converting the same string back to QuestionNode Object..at that time it is giving the exception..i tried commenting the line of QuestionNode(String Value, int? QId, string QuesDiscription, string QuesType, string Editable, int? PrevQues, int UseResponse, int UserSelected, int Progress, List<QuestionNode> OptionSet){} .it is not working .after adding default constructor it is creating object with all the fields having null values Commented Nov 23, 2015 at 9:47

1 Answer 1

1

As the exception states you have to add a default constructor:

class QuestionNode
{
    QuesionNode(){} //Empty default constructor for deserializer

    public String Value { get; set; }
    public int? QId { get; set; }
    //More properties....
}
Sign up to request clarification or add additional context in comments.

2 Comments

Although it is the case in your answer, I would explicitly point out, that this ctor can be made private which leaves the encapsulation as desired by the OP.
after adding the default constructor ,it create a json with all the fields null..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.