如何在 Visual Basic 2013 中反序列化 Json?
How to Deserialize Json in Visual Basic 2013?
我正在尝试将反序列化的 json 数据插入 Visual Basic 2013 中的 sqlite table。但是,我在反序列化 json 数据时遇到问题。 Json数据结构如下-
[
{
"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"
}
]
我目前的代码(点击按钮时)是 -
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
但它抛出以下错误 -
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
看了网上和Whosebug的类似问题后,我做了JSON_Questions
class如下-
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
之前,我使用 class 如下 -
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
我什至尝试过 JObject
和 List(of JToken)
-
Dim jResults As JObject = JObject.Parse(rawresp)
Dim results As List(Of JToken) = jResults.Children().ToList()
但是,上面的 none 似乎有效。 我被困在这里超过 2 天,但无法弄清楚如何反序列化这个 Json 并插入到 sqlite table.
错误信息的关键部分是:
change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List
这两种方式中的任何一种都可以很好地处理更正后的数据:
' get a List<T>
Dim myQAs = JsonConvert.DeserializeObject(Of List(Of JSON_Questions))(jstr)
' deser to an array:
Dim myQAs = JsonConvert.DeserializeObject(Of JSON_Questions())(jstr)
发布的 Json 包含 两个 问题,这就是为什么错误消息说它需要一个数组 r 列表。现在,当反序列化时,它位于数组或列表中,具体取决于您使用的是哪个,因此要访问您的数据:
thisQ = myQAs(0).Question_Text
键入点后,Intellisense 将显示该字段,您可以选择所需内容。
如果 Option_01
...etc 是 Option 的集合或数组,而不是一遍又一遍地拼写出来,那么整个事情会更容易处理。
这是关键:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'Quiz_Maker.JSON_Questions' because the type requires a JSON object
反序列化器需要一个 JSON 对象,但您传递的是一个 JSON 数组。只需删除开头和结尾的方括号,您的代码就会开始运行。
或者你也可以使用
Dim jResults As JArray = JArray.Parse(rawresp)
而不是
Dim jResults As JObject = JObject.Parse(rawresp)
希望这对您有所帮助,尽管它未经测试。
我正在尝试将反序列化的 json 数据插入 Visual Basic 2013 中的 sqlite table。但是,我在反序列化 json 数据时遇到问题。 Json数据结构如下-
[
{
"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"
}
]
我目前的代码(点击按钮时)是 -
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
但它抛出以下错误 -
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
看了网上和Whosebug的类似问题后,我做了JSON_Questions
class如下-
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
之前,我使用 class 如下 -
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
我什至尝试过 JObject
和 List(of JToken)
-
Dim jResults As JObject = JObject.Parse(rawresp)
Dim results As List(Of JToken) = jResults.Children().ToList()
但是,上面的 none 似乎有效。 我被困在这里超过 2 天,但无法弄清楚如何反序列化这个 Json 并插入到 sqlite table.
错误信息的关键部分是:
change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List
这两种方式中的任何一种都可以很好地处理更正后的数据:
' get a List<T>
Dim myQAs = JsonConvert.DeserializeObject(Of List(Of JSON_Questions))(jstr)
' deser to an array:
Dim myQAs = JsonConvert.DeserializeObject(Of JSON_Questions())(jstr)
发布的 Json 包含 两个 问题,这就是为什么错误消息说它需要一个数组 r 列表。现在,当反序列化时,它位于数组或列表中,具体取决于您使用的是哪个,因此要访问您的数据:
thisQ = myQAs(0).Question_Text
键入点后,Intellisense 将显示该字段,您可以选择所需内容。
如果 Option_01
...etc 是 Option 的集合或数组,而不是一遍又一遍地拼写出来,那么整个事情会更容易处理。
这是关键:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Quiz_Maker.JSON_Questions' because the type requires a JSON object
反序列化器需要一个 JSON 对象,但您传递的是一个 JSON 数组。只需删除开头和结尾的方括号,您的代码就会开始运行。
或者你也可以使用
Dim jResults As JArray = JArray.Parse(rawresp)
而不是
Dim jResults As JObject = JObject.Parse(rawresp)
希望这对您有所帮助,尽管它未经测试。