Hi, I am a real API newbie and I need to deserialize a quite complex Json from an IOT provider but I am stuck from long time on this issue: With the attached code I get strange results : I get Network.name instead than expected Unit.name. Furthermore: Unit is supposed to be an array but if I try to deserialize it as array I get the exception: "Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Web_request.API_BBL+Unit]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly." Classes was generated with paste as Json Classes Vb.net function. Thanks so much for any kind advise, all the best link to json file: https://drive.google.com/open?id=1kXYcoSJE-aAJWX1WR6wjOB4r6AFikhRh
Imports System Imports System.Dynamic Imports System.Text Imports System.IO Imports System.Net.Http Imports System.Threading.Tasks Imports RestSharp.Authenticators Imports Web_request Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq Imports RestSharp Public Class API_BBL Public Class Rootobject Public Property id As String Public Property revision As Integer Public Property generation As Integer Public Property lastUsedProtocol As Integer Public Property channels As Integer Public Property mode As Integer Public Property grade As Integer Public Property uuid As String Public Property gateway As Gateway Public Property email As String Public Property name As String Public Property visitorKey As String Public Property managerKey As String Public Property type As Integer Public Property nextSceneID As Integer Public Property nextUnitID As Integer Public Property nextEventID As Integer Public Property nextControlID As Integer Public Property nextGroupID As Integer Public Property protocolVersion As Integer Public Property allUnitPosition As Integer Public Property nearUnitPosition As Integer Public Property nearbyEnabled As Boolean Public Property allowPrototypes As Boolean Public Property longitude As Single Public Property latitude As Single Public Property timeZone As String Public Property settings As Settings Public Property grid As Grid Public Property photos() As Photo Public Property units() As Unit Public Property scenes() As Scene Public Property events() As _Event Public Property circadianProfiles() As Circadianprofile End Class Public Class Gateway End Class Public Class Settings Public Property revision As Integer Public Property control As Control End Class Public Class Control Public Property useLastLevels As Boolean Public Property enabled As Boolean Public Property activateTimersAfterStartup As Boolean Public Property startupDuration As Integer Public Property tapDuration As Integer Public Property dimDuration As Integer Public Property fadeout As Integer Public Property behaviour As Integer Public Property weekdays() As Weekday End Class Public Class Weekday Public Property weekday As Integer Public Property dayStart As Integer Public Property dayEnd As Integer Public Property dayTimeout As Integer Public Property nightTimeout As Integer End Class Public Class Grid Public Property type As Integer Public Property position As Integer Public Property groupID As Integer Public Property cells() As Cell End Class Public Class Cell Public Property type As Integer Public Property position As Integer Public Property groupID As Integer Public Property unit As Integer End Class Public Class Photo Public Property position As Integer Public Property height As Single Public Property controls() As Object Public Property image As String End Class Public Class Unit Public Property deviceID As Integer Public Property address As String Public Property cpu As Integer Public Property name As String Public Property type As Integer Public Property appearance As Integer Public Property hueID As Integer Public Property groupID As Integer Public Property position As Integer Public Property revision As Integer Public Property configRevision As Integer Public Property firmware As Integer Public Property modes() As Mode Public Property smartSwitch As Smartswitch Public Property pushButtonToggleDisabled As Boolean Public Property presenceSensor As Presencesensor Public Property linkedSensor As Integer Public Property gain As Single Public Property startupMode As Integer Public Property minOnLevel As Single Public Property minLevel As Single Public Property maxLevel As Single Public Property parameters() As Object Public Property sequenceCounter As Integer Public Property behaviour As Integer Public Property timeout As Integer Public Property labels As Labels Public Property details As Details Public Property actionConfig As Integer Public Property actions() As Object Public Property pushButton As Pushbutton Public Property switchConfig As Switchconfig Public Property securityKey As String Public Property pushButton2 As Pushbutton2 Public Property pushButton3 As Pushbutton3 Public Property pushButton4 As Pushbutton4 End Class Public Class Smartswitch Public Property type As Integer Public Property id As Integer Public Property scenes() As Object Public Property scenes2() As Object Public Property group As Integer Public Property unit As Integer Public Property lingerTime As Integer Public Property fadeTime As Integer Public Property excludeOFF As Boolean Public Property index As Integer End Class Public Class Presencesensor Public Property type As Integer Public Property id As Integer Public Property scenes() As Object Public Property scenes2() As Object Public Property group As Integer Public Property unit As Integer Public Property lingerTime As Integer Public Property fadeTime As Integer Public Property excludeOFF As Boolean Public Property index As Integer End Class Public Class Labels End Class Public Class Details End Class Public Class Pushbutton Public Property type As Integer Public Property id As Integer Public Property scenes() As Integer? Public Property scenes2() As Object Public Property group As Integer Public Property unit As Integer Public Property lingerTime As Integer Public Property fadeTime As Integer Public Property excludeOFF As Boolean Public Property index As Integer End Class Public Class Switchconfig Public Property buttons() As Button Public Property switches() As Object Public Property exclusiveScenes As Boolean Public Property longPressAllOff As Boolean Public Property toggleDisabled As Boolean End Class Public Class Button Public Property type As Integer Public Property action As Integer Public Property target As Integer End Class Public Class Pushbutton2 Public Property type As Integer Public Property id As Integer Public Property scenes() As Object Public Property scenes2() As Object Public Property group As Integer Public Property unit As Integer Public Property lingerTime As Integer Public Property fadeTime As Integer Public Property excludeOFF As Boolean Public Property index As Integer End Class Public Class Pushbutton3 Public Property type As Integer Public Property id As Integer Public Property scenes() As Integer Public Property scenes2() As Object Public Property group As Integer Public Property unit As Integer Public Property lingerTime As Integer Public Property fadeTime As Integer Public Property excludeOFF As Boolean Public Property index As Integer End Class Public Class Pushbutton4 Public Property type As Integer Public Property id As Integer Public Property scenes() As Integer Public Property scenes2() As Object Public Property group As Integer Public Property unit As Integer Public Property lingerTime As Integer Public Property fadeTime As Integer Public Property excludeOFF As Boolean Public Property index As Integer End Class Public Class Mode Public Property name As String Public Property state As String Public Property cycle As Boolean End Class Public Class Scene Public Property name As String Public Property sceneID As Integer Public Property siteSceneID As Integer Public Property revision As Integer Public Property icon As Integer Public Property color As Integer Public Property position As Integer Public Property type As Integer Public Property repeat As Boolean Public Property hidden As Boolean Public Property units() As Unit1 Public Property steps() As _Step Public Property conditions() As Object Public Property sensors() As Object Public Property mode As Integer Public Property circadianProfile As Integer Public Property stayOn As Boolean End Class Public Class Unit1 Public Property unit As Integer Public Property state As String End Class Public Class _Step Public Property scene As Integer Public Property level As Single Public Property duration As Integer End Class Public Class _Event Public Property enabled As Boolean Public Property overridePresence As Boolean Public Property id As Integer Public Property siteid As Integer Public Property type As Integer Public Property revision As Integer Public Property startTime As Starttime Public Property endTime As Endtime Public Property duration As Integer Public Property fadeTime As Integer Public Property scenes() As Integer Public Property targets() As Target End Class Public Class Starttime Public Property dateType As Integer Public Property timeType As Integer Public Property weekdays As Integer Public Property year As Integer Public Property month As Integer Public Property day As Integer Public Property hour As Integer Public Property minute As Integer Public Property offset As Integer End Class Public Class Endtime Public Property dateType As Integer Public Property timeType As Integer Public Property weekdays As Integer Public Property year As Integer Public Property month As Integer Public Property day As Integer Public Property hour As Integer Public Property minute As Integer Public Property offset As Integer End Class Public Class Target Public Property scene As Integer Public Property level As Single Public Property duration As Integer End Class Public Class Circadianprofile Public Property id As Integer Public Property revision As Integer Public Property name As String Public Property graph As Graph End Class Public Class Graph Public Property type As Integer Public Property points() As Point End Class Public Class Point Public Property x As Single Public Property y As Single End Class Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim fileReader As String fileReader = My.Computer.FileSystem.ReadAllText("C:\Json.txt") Resp_API.Text = fileReader Dim jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Unit)(fileReader) TextBox1.Text = jsonObj.name End Sub End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)