Click here to Skip to main content
15,942,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


What I have tried:

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
Posted
Comments
#realJSOP 15-Jul-19 14:14pm    
You have to post the json you're trying to deserialize. All we can contribute at this point is, "Yep! looks OK to me."
F-ES Sitecore 16-Jul-19 4:13am    
Rather than trying it all at once it might be easier to break it down and get small sections of json deserialised, and once you have a small bit working move on to get other bits working. If there is json that doesn't match your classes it is just ignored, you don't need an exact match, so that will let you expand your classes a bit at a time, solving smaller problems as you go and you will eventually build up to the whole file.

Alternatively there are on-line tools that you can paste your json to and they will generate class structures for you.
Member 9992334 16-Jul-19 7:47am    
Sorry, apologize I attached wrong Json...
This is the small section:
{"units":[{"deviceID":14,"address":"c29419a7442d","cpu":2,"name":"Scale campo","type":526,"appearance":0,"hueID":0,"groupID":0,"position":3,"revision":1,"configRevision":2,"firmware":6656,"modes":[{"name":"unit_mode1","state":"fc03","cycle":true},{"name":"unit_mode2","state":"0000","cycle":false},{"name":"unit_mode3","state":"0000","cycle":false},{"name":"unit_mode4","state":"0000","cycle":false}],"smartSwitch":{"type":0,"id":0,"scenes":[],"scenes2":[],"group":0,"unit":0,"lingerTime":120,"fadeTime":10,"excludeOFF":false,"index":0},"pushButtonToggleDisabled":false,"presenceSensor":{"type":5,"id":0,"scenes":[],"scenes2":[],"group":0,"unit":0,"lingerTime":120,"fadeTime":10,"excludeOFF":false,"index":0},"linkedSensor":0,"gain":100,"startupMode":1,"minOnLevel":0,"minLevel":0,"maxLevel":1,"parameters":[],"sequenceCounter":0,"behaviour":0,"timeout":0,"labels":{},"details":{},"actionConfig":0,"actions":[]},{"deviceID":15,"address":"b12897c544fa","cpu":2,"name":"Scale ingresso","type":526,"appearance":0,"hueID":0,"groupID":0,"position":3,"revision":1,"configRevision":3,"firmware":6656,"modes":[{"name":"unit_mode1","state":"0000","cycle":true},{"name":"unit_mode2","state":"0000","cycle":false},{"name":"unit_mode3","state":"0000","cycle":false},{"name":"unit_mode4","state":"0000","cycle":false}],"smartSwitch":{"type":0,"id":0,"scenes":[],"scenes2":[],"group":0,"unit":0,"lingerTime":120,"fadeTime":10,"excludeOFF":false,"index":0},"pushButtonToggleDisabled":false,"presenceSensor":{"type":5,"id":0,"scenes":[],"scenes2":[],"group":0,"unit":0,"lingerTime":120,"fadeTime":10,"excludeOFF":false,"index":0},"linkedSensor":0,"gain":100,"startupMode":1,"minOnLevel":0,"minLevel":0,"maxLevel":1,"parameters":[],"sequenceCounter":0,"behaviour":0,"timeout":0,"labels":{},"details":{},"actionConfig":0,"actions":[]}]}
Member 9992334 17-Jul-19 4:54am    
Finally I figured out that issues were the classes that was not coorrectly generated due to empty arrays.
Thanks a lot anyway for your help.

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 Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\Json6.txt")

Resp_API.Text = fileReader

Dim jsonObj = JsonConvert.DeserializeObject(Of Rootobject)(fileReader)

Dim root = jsonObj.units.Item(9)
TextBox1.Text = root.name


End Sub


End Class



Public Class RootObject
Public Property units As List(Of Unit)
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 List(Of Mode)
Public Property smartSwitch As Device
Public Property pushButtonToggleDisabled As Boolean
Public Property presenceSensor As Device
Public Property linkedSensor As Integer
Public Property gain As Integer
Public Property startupMode As Integer
Public Property minOnLevel As Integer
Public Property minLevel As Integer
Public Property maxLevel As Integer
Public Property parameters As List(Of String)
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 List(Of String)
End Class

Public Class Device
Public Property type As Integer
Public Property id As Integer
Public Property scenes As List(Of String)
Public Property scenes2 As List(Of String)
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 Mode
Public Property name As String
Public Property state As String
Public Property cycle As Boolean
End Class

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900