I need to create a nested JSON for a Trivia application using WebAPI. My Database structure is
TriviaQuiz - A Quiz can have many questions. TriviaQuestion - A Question can have many options (choices) TriviaOption - List of answers (choices)
I need to Produce JSON in below Format
{
"quiz" : [{
"id" : 1,
"name" :"Guess Fruit Color",
"description": "You need to guess color of fruits",
"questions" : [
{"id" : 1000 , "quizId" : 1 , "description": "The color of apple is" , "options" : [
{"id" : 1 , "questionId" : 1000 , "description" : "Green", "correctAnswer" : false },
{"id" : 2 , "questionId" : 1000 , "description" : "Red", "correctAnswer" : true },
{"id" : 3 , "questionId" : 1000 , "description" : "Pink", "correctAnswer" : false },
{"id" : 4 , "questionId" : 1000 , "description" : "Purple", "correctAnswer": false }
]
},
{"id" : 1001 , "quizId" : 1 , "description": "The color of mangoss is" , "options" : [
{"id" : 5 , "questionId" : 1001 , "description" : "Green", "correctAnswer" : false },
{"id" : 6 , "questionId" : 1001 , "description" : "Red", "correctAnswer" : false },
{"id" : 6 , "questionId" : 1001 , "description" : "Yello", "correctAnswer" : true },
{"id" : 8 , "questionId" : 1001 , "description" : "Purple", "correctAnswer": false }
]
}
]}
]
}
I have designed my class like
public class TriviaQuiz
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
//A Quiz can have many questions
public virtual List<TriviaQuestion> questions { get; set; }
}
public class TriviaQuestion
{
[Key(), ForeignKey("triviaQuiz")]
public int id { get; set; }
public string description { get; set; }
//Navigation Property to set foreign key relationship
[JsonIgnore]
public virtual TriviaQuiz triviaQuiz { get; set; }
//A question can have many options
public virtual List<TriviaOption> options { get; set; }
}
public class TriviaOption
{
public int id { get; set; }
[ForeignKey("triviaQuestion")]
//Tell EF That questionId is a Foreign key to TriviaQuestion table
public int questionId { get; set; }
public string description { get; set; }
public Boolean correctAnswer { get; set; }
//Navigation Property to set up Foreign key relation ship
[JsonIgnore]
public virtual TriviaQuestion triviaQuestion { get; set; }
}
My WebAPI Looks like this
public List<TriviaQuiz> getTopJson()
{
return _db.TriviaQuizs.ToList();
}
Can some some suggest what is the best approach to generate JSON. Should I be creating a ViewModel for this?
When I run this I get an error: One or more validation errors were detected during model generation:
TriviaQuestion_triviaQuiz_Source: : Multiplicity is not valid in Role 'TriviaQuestion_triviaQuiz_Source' in relationship 'TriviaQuestion_triviaQuiz'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be 'enter code here1'.
I have already spend a week researching but not able to find anything relevant on the internet, Can some one help.