I am trying to insert deserialized json data into sqlite table in Visual Basic 2013. But, I am facing problem in Deserializing json data. Json data structure is as follows -
[
{
"QID":"1",
"Question_Text":"This is a demo question",
"Question_Media":null,
"Subject_Id":"1",
"Subject_Name":"Demo Subject",
"Topic_Id":"12",
"Topic_Name":"Demo Topic",
"Chapter_No":"11",
"Exam_Id":"58",
"Exam_Name":"Demo Exam",
"Explanation":"Demo Explanation",
"Explanation_Media":null,
"Reference":null,
"Option_01":"Lesser petrosal nerve",
"Option_01_Media":null,
"Option_01_isCorrect":"0",
"Option_01_Hint":null,
"Option_01_Hint_Media":null,
"Option_02":"Maxillary nerve",
"Option_02_Media":null,
"Option_02_isCorrect":"1",
"Option_02_Hint":null,
"Option_02_Hint_Media":null,
"Option_03":"Motor root of trigeminal nerve",
"Option_03_Media":null,
"Option_03_isCorrect":"0",
"Option_03_Hint":null,
"Option_03_Hint_Media":null,
"Option_04":"Sensory branch of mandibular nerve",
"Option_04_Media":null,
"Option_04_isCorrect":"0",
"Option_04_Hint":null,
"Option_04_Hint_Media":null,
"Option_05":null,
"Option_05_Media":null,
"Option_05_isCorrect":"0",
"Option_05_Hint":null,
"Option_05_Hint_Media":null,
"Option_06":null,
"Option_06_Media":null,
"Option_06_isCorrect":"0",
"Option_06_Hint":null,
"Option_06_Hint_Media":null,
"Added_By":"demo",
"Question_Type":"1",
"Created_At":"2015-03-07 22:58:01",
"Updated_At":"2015-03-14 22:56:08",
"DID":"0",
"Exams":null,
"Difficulty_Level":"1",
"isVerified":"0",
"isDuplicate":"0",
"isSeen":"0",
"isAnsweredCorrect":"0",
"isAnsweredWrong":"0",
"isMarked":"0",
"Remarks":null,
"isRepeat":"0",
"NotInExam":"0"
},
{
"QID":"2",
"Question_Text":"Demo question 2",
"Question_Media":null,
"Subject_Id":"1",
"Subject_Name":"Demo subject 2",
"Topic_Id":"12",
"Topic_Name":"Demo topic 2",
"Chapter_No":"11",
"Exam_Id":"58",
"Exam_Name":"Demo exam 2",
"Explanation":"Demo explanation 2",
"Explanation_Media":null,
"Reference":null,
"Option_01":"Hypothalamus",
"Option_01_Media":null,
"Option_01_isCorrect":"0",
"Option_01_Hint":null,
"Option_01_Hint_Media":null,
"Option_02":"Pituitary gland",
"Option_02_Media":null,
"Option_02_isCorrect":"1",
"Option_02_Hint":null,
"Option_02_Hint_Media":null,
"Option_03":"Amygdaloid body",
"Option_03_Media":null,
"Option_03_isCorrect":"0",
"Option_03_Hint":null,
"Option_03_Hint_Media":null,
"Option_04":"Basal ganglia ",
"Option_04_Media":null,
"Option_04_isCorrect":"0",
"Option_04_Hint":null,
"Option_04_Hint_Media":null,
"Option_05":null,
"Option_05_Media":null,
"Option_05_isCorrect":"0",
"Option_05_Hint":null,
"Option_05_Hint_Media":null,
"Option_06":null,
"Option_06_Media":null,
"Option_06_isCorrect":"0",
"Option_06_Hint":null,
"Option_06_Hint_Media":null,
"Added_By":"demo",
"Question_Type":"1",
"Created_At":"2015-03-07 22:59:15",
"Updated_At":"2015-03-14 21:42:12",
"DID":"0",
"Exams":null,
"Difficulty_Level":"1",
"isVerified":"0",
"isDuplicate":"0",
"isSeen":"0",
"isAnsweredCorrect":"0",
"isAnsweredWrong":"0",
"isMarked":"0",
"Remarks":null,
"isRepeat":"0",
"NotInExam":"0"
}
]
My code so far (on button click) is -
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Dim rawresp As String
Dim URL As String
URL = "http://localhost/json/question.php"
Try
request = DirectCast(WebRequest.Create(URL), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
rawresp = reader.ReadToEnd()
'Everything works fine until here, but error occurs as soon as I write following code
Dim jsonQuestions As JSON_Questions = _
JsonConvert.DeserializeObject(Of JSON_Questions)(rawresp.ToString)
Catch ex As Exception
TextBox1.Text = ex.ToString
Finally
If Not response Is Nothing Then response.Close()
End Try
But it is throwing following error -
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Quiz_Maker.JSON_Questions' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) at Quiz_Maker.Advanced_Settings.btnResetQuestions_Click(Object sender, EventArgs e) in C:\VisualStudioProjects\Quiz Maker\Quiz Maker\Quiz Maker\Advanced Settings.vb:line 25
After reading on net and stackoverflow's similar questions, I made the JSON_Questions
class as follows -
Public Class JSON_Questions
Public Property QID() As String
Get
Return m_QID
End Get
Set(value As String)
m_QID = value
End Set
End Property
Private m_QID As String
Public Property Question_Text() As String
Get
Return m_Question_Text
End Get
Set(value As String)
m_Question_Text = value
End Set
End Property
Private m_Question_Text As String
Public Property Question_Media() As Object
Get
Return m_Question_Media
End Get
Set(value As Object)
m_Question_Media = value
End Set
End Property
Private m_Question_Media As Object
Public Property Subject_Id() As String
Get
Return m_Subject_Id
End Get
Set(value As String)
m_Subject_Id = value
End Set
End Property
Private m_Subject_Id As String
Public Property Subject_Name() As String
Get
Return m_Subject_Name
End Get
Set(value As String)
m_Subject_Name = value
End Set
End Property
Private m_Subject_Name As String
Public Property Topic_Id() As String
Get
Return m_Topic_Id
End Get
Set(value As String)
m_Topic_Id = value
End Set
End Property
Private m_Topic_Id As String
Public Property Topic_Name() As String
Get
Return m_Topic_Name
End Get
Set(value As String)
m_Topic_Name = value
End Set
End Property
Private m_Topic_Name As String
Public Property Chapter_No() As String
Get
Return m_Chapter_No
End Get
Set(value As String)
m_Chapter_No = value
End Set
End Property
Private m_Chapter_No As String
Public Property Exam_Id() As String
Get
Return m_Exam_Id
End Get
Set(value As String)
m_Exam_Id = value
End Set
End Property
Private m_Exam_Id As String
Public Property Exam_Name() As String
Get
Return m_Exam_Name
End Get
Set(value As String)
m_Exam_Name = value
End Set
End Property
Private m_Exam_Name As String
Public Property Explanation() As String
Get
Return m_Explanation
End Get
Set(value As String)
m_Explanation = value
End Set
End Property
Private m_Explanation As String
Public Property Explanation_Media() As Object
Get
Return m_Explanation_Media
End Get
Set(value As Object)
m_Explanation_Media = value
End Set
End Property
Private m_Explanation_Media As Object
Public Property Reference() As Object
Get
Return m_Reference
End Get
Set(value As Object)
m_Reference = value
End Set
End Property
Private m_Reference As Object
Public Property Option_01() As String
Get
Return m_Option_01
End Get
Set(value As String)
m_Option_01 = value
End Set
End Property
Private m_Option_01 As String
Public Property Option_01_Media() As Object
Get
Return m_Option_01_Media
End Get
Set(value As Object)
m_Option_01_Media = value
End Set
End Property
Private m_Option_01_Media As Object
Public Property Option_01_isCorrect() As String
Get
Return m_Option_01_isCorrect
End Get
Set(value As String)
m_Option_01_isCorrect = value
End Set
End Property
Private m_Option_01_isCorrect As String
Public Property Option_01_Hint() As String
Get
Return m_Option_01_Hint
End Get
Set(value As String)
m_Option_01_Hint = value
End Set
End Property
Private m_Option_01_Hint As String
Public Property Option_01_Hint_Media() As Object
Get
Return m_Option_01_Hint_Media
End Get
Set(value As Object)
m_Option_01_Hint_Media = value
End Set
End Property
Private m_Option_01_Hint_Media As Object
Public Property Option_02() As String
Get
Return m_Option_02
End Get
Set(value As String)
m_Option_02 = value
End Set
End Property
Private m_Option_02 As String
Public Property Option_02_Media() As Object
Get
Return m_Option_02_Media
End Get
Set(value As Object)
m_Option_02_Media = value
End Set
End Property
Private m_Option_02_Media As Object
Public Property Option_02_isCorrect() As String
Get
Return m_Option_02_isCorrect
End Get
Set(value As String)
m_Option_02_isCorrect = value
End Set
End Property
Private m_Option_02_isCorrect As String
Public Property Option_02_Hint() As Object
Get
Return m_Option_02_Hint
End Get
Set(value As Object)
m_Option_02_Hint = value
End Set
End Property
Private m_Option_02_Hint As Object
Public Property Option_02_Hint_Media() As Object
Get
Return m_Option_02_Hint_Media
End Get
Set(value As Object)
m_Option_02_Hint_Media = value
End Set
End Property
Private m_Option_02_Hint_Media As Object
Public Property Option_03() As String
Get
Return m_Option_03
End Get
Set(value As String)
m_Option_03 = value
End Set
End Property
Private m_Option_03 As String
Public Property Option_03_Media() As Object
Get
Return m_Option_03_Media
End Get
Set(value As Object)
m_Option_03_Media = value
End Set
End Property
Private m_Option_03_Media As Object
Public Property Option_03_isCorrect() As String
Get
Return m_Option_03_isCorrect
End Get
Set(value As String)
m_Option_03_isCorrect = value
End Set
End Property
Private m_Option_03_isCorrect As String
Public Property Option_03_Hint() As Object
Get
Return m_Option_03_Hint
End Get
Set(value As Object)
m_Option_03_Hint = value
End Set
End Property
Private m_Option_03_Hint As Object
Public Property Option_03_Hint_Media() As Object
Get
Return m_Option_03_Hint_Media
End Get
Set(value As Object)
m_Option_03_Hint_Media = value
End Set
End Property
Private m_Option_03_Hint_Media As Object
Public Property Option_04() As String
Get
Return m_Option_04
End Get
Set(value As String)
m_Option_04 = value
End Set
End Property
Private m_Option_04 As String
Public Property Option_04_Media() As Object
Get
Return m_Option_04_Media
End Get
Set(value As Object)
m_Option_04_Media = value
End Set
End Property
Private m_Option_04_Media As Object
Public Property Option_04_isCorrect() As String
Get
Return m_Option_04_isCorrect
End Get
Set(value As String)
m_Option_04_isCorrect = value
End Set
End Property
Private m_Option_04_isCorrect As String
Public Property Option_04_Hint() As Object
Get
Return m_Option_04_Hint
End Get
Set(value As Object)
m_Option_04_Hint = value
End Set
End Property
Private m_Option_04_Hint As Object
Public Property Option_04_Hint_Media() As Object
Get
Return m_Option_04_Hint_Media
End Get
Set(value As Object)
m_Option_04_Hint_Media = value
End Set
End Property
Private m_Option_04_Hint_Media As Object
Public Property Option_05() As String
Get
Return m_Option_05
End Get
Set(value As String)
m_Option_05 = value
End Set
End Property
Private m_Option_05 As String
Public Property Option_05_Media() As Object
Get
Return m_Option_05_Media
End Get
Set(value As Object)
m_Option_05_Media = value
End Set
End Property
Private m_Option_05_Media As Object
Public Property Option_05_isCorrect() As String
Get
Return m_Option_05_isCorrect
End Get
Set(value As String)
m_Option_05_isCorrect = value
End Set
End Property
Private m_Option_05_isCorrect As String
Public Property Option_05_Hint() As Object
Get
Return m_Option_05_Hint
End Get
Set(value As Object)
m_Option_05_Hint = value
End Set
End Property
Private m_Option_05_Hint As Object
Public Property Option_05_Hint_Media() As Object
Get
Return m_Option_05_Hint_Media
End Get
Set(value As Object)
m_Option_05_Hint_Media = value
End Set
End Property
Private m_Option_05_Hint_Media As Object
Public Property Option_06() As String
Get
Return m_Option_06
End Get
Set(value As String)
m_Option_06 = value
End Set
End Property
Private m_Option_06 As String
Public Property Option_06_Media() As Object
Get
Return m_Option_06_Media
End Get
Set(value As Object)
m_Option_06_Media = value
End Set
End Property
Private m_Option_06_Media As Object
Public Property Option_06_isCorrect() As String
Get
Return m_Option_06_isCorrect
End Get
Set(value As String)
m_Option_06_isCorrect = value
End Set
End Property
Private m_Option_06_isCorrect As String
Public Property Option_06_Hint() As Object
Get
Return m_Option_06_Hint
End Get
Set(value As Object)
m_Option_06_Hint = value
End Set
End Property
Private m_Option_06_Hint As Object
Public Property Option_06_Hint_Media() As Object
Get
Return m_Option_06_Hint_Media
End Get
Set(value As Object)
m_Option_06_Hint_Media = value
End Set
End Property
Private m_Option_06_Hint_Media As Object
Public Property Added_By() As String
Get
Return m_Added_By
End Get
Set(value As String)
m_Added_By = value
End Set
End Property
Private m_Added_By As String
Public Property Question_Type() As String
Get
Return m_Question_Type
End Get
Set(value As String)
m_Question_Type = value
End Set
End Property
Private m_Question_Type As String
Public Property Created_At() As String
Get
Return m_Created_At
End Get
Set(value As String)
m_Created_At = value
End Set
End Property
Private m_Created_At As String
Public Property Updated_At() As String
Get
Return m_Updated_At
End Get
Set(value As String)
m_Updated_At = value
End Set
End Property
Private m_Updated_At As String
Public Property DID() As String
Get
Return m_DID
End Get
Set(value As String)
m_DID = value
End Set
End Property
Private m_DID As String
Public Property Exams() As String
Get
Return m_Exams
End Get
Set(value As String)
m_Exams = value
End Set
End Property
Private m_Exams As String
Public Property Difficulty_Level() As String
Get
Return m_Difficulty_Level
End Get
Set(value As String)
m_Difficulty_Level = value
End Set
End Property
Private m_Difficulty_Level As String
Public Property isVerified() As String
Get
Return m_isVerified
End Get
Set(value As String)
m_isVerified = value
End Set
End Property
Private m_isVerified As String
Public Property isDuplicate() As String
Get
Return m_isDuplicate
End Get
Set(value As String)
m_isDuplicate = value
End Set
End Property
Private m_isDuplicate As String
Public Property isSeen() As String
Get
Return m_isSeen
End Get
Set(value As String)
m_isSeen = value
End Set
End Property
Private m_isSeen As String
Public Property isAnsweredCorrect() As String
Get
Return m_isAnsweredCorrect
End Get
Set(value As String)
m_isAnsweredCorrect = value
End Set
End Property
Private m_isAnsweredCorrect As String
Public Property isAnsweredWrong() As String
Get
Return m_isAnsweredWrong
End Get
Set(value As String)
m_isAnsweredWrong = value
End Set
End Property
Private m_isAnsweredWrong As String
Public Property isMarked() As String
Get
Return m_isMarked
End Get
Set(value As String)
m_isMarked = value
End Set
End Property
Private m_isMarked As String
Public Property Remarks() As Object
Get
Return m_Remarks
End Get
Set(value As Object)
m_Remarks = value
End Set
End Property
Private m_Remarks As Object
Public Property isRepeat() As String
Get
Return m_isRepeat
End Get
Set(value As String)
m_isRepeat = value
End Set
End Property
Private m_isRepeat As String
Public Property NotInExam() As String
Get
Return m_NotInExam
End Get
Set(value As String)
m_NotInExam = value
End Set
End Property
Private m_NotInExam As String
End Class
Previously, I was using the class as follows -
Public Class JSON_Questions
Public QID As Integer
Public Question_Text As String
Public Subject_Id As Integer
Public Subject_Name As String
Public Topic_Id As Integer
Public Topic_Name As String
Public Chapter_No As Integer
Public Exam_Id As Integer
Public Exam_Name As String
Public Explanation As String
Public Option_01 As String
Public Option_01_isCorrect As Integer
Public Option_02 As String
Public Option_02_isCorrect As Integer
Public Option_03 As String
Public Option_03_isCorrect As Integer
Public Option_04 As String
Public Option_04_isCorrect As Integer
Public Option_05 As String
Public Option_05_isCorrect As Integer
Public Option_06 As String
Public Option_06_isCorrect As Integer
Public Added_By As String
Public Question_Type As Integer
Public Created_At As String
Public Updated_At As String
'Public Json_Result As String
End Class
I have even tried JObject
and List(of JToken)
-
Dim jResults As JObject = JObject.Parse(rawresp)
Dim results As List(Of JToken) = jResults.Children().ToList()
But, none of the above seems to be working. I am stuck here for more than 2 days, but could not figure out how to Deserialize this Json and insert into sqlite table.