JSON不反序列化为自定义类

时间:2019-01-29 12:14:32

标签: vb.net json.net

这是我第一次使用JSON。我已经将一个自定义类序列化为一个List,但是不会反序列化。任何帮助表示赞赏

Dim Appointments As New List(Of UserAppointments)()
Using dt As DataTable = GetUserAppointments(ApptRequest.UserID, ApptRequest.PeriodFrom, ApptRequest.PeriodUntil)
For Each appt As DataRow In dt.Rows
    Appointments.Add(New UserAppointments With {.AppointmentID = CType(appt.Item("UniqueID").ToString, Long)})
    Next
End Using
Dim JSON As String = js.Serialize(JsonConvert.SerializeObject(Appointments))

产生

  

“ [{\” AppointmentID \“:33},{\” AppointmentID \“:34},{\” AppointmentID \“:35},{\” AppointmentID \“:36},{\” AppointmentID \ “:37},{\” AppointmentID \“:38},{\” AppointmentID \“:39},{\” AppointmentID \“:40},{\” AppointmentID \“:373},{\” AppointmentID \ “:374},{\” AppointmentID \“:375},{\” AppointmentID \“:376},{\” AppointmentID \“:377},{\” AppointmentID \“:687}]”

当我尝试再次反序列化

Dim rslt As List(Of UserAppointments) = JsonConvert.DeserializeObject(Of List(Of UserAppointments))(JSON)

我收到以下错误

  

转换值“ [{{AppointmentID”:33},{“ AppointmentID”:34},{“ AppointmentID”:35},{“ AppointmentID”:36},{“ AppointmentID”:37},{“ AppointmentID”:38},{“ AppointmentID”:39},{“ AppointmentID”:40},{“ AppointmentID”:373},{“ AppointmentID”:374},{“ AppointmentID”:375},{“ AppointmentID” :376},{“ AppointmentID”:377},{“ AppointmentID”:687}]”以键入“ System.Collections.Generic.List`1 [NetworkRequests + UserAppointments]”。路径‘’,第1行,位置331。

1 个答案:

答案 0 :(得分:0)

我相信您的问题出在这条线上。您要对其进行两次序列化。

  

将JSON设为字符串=   js.Serialize(JsonConvert.SerializeObject(Appointments))

您刚刚尝试过

  

将JSON设为字符串=   JsonConvert.SerializeObject(Appointments)

还是有理由将其加倍?