Search code examples
vb.netjsonserializer

Serialize json - Unable to cast object of type 'XXX' to type 'System.Collections.IEnumerable'


Trying to serialize json with the following:

<JsonArray>
Public Class NumbersItem
    Public Sub New(objTime As Long, objSolValue As Long, objUsageValue As Long)
        DetectionTime = objTime
        SolValue = objSolValue
        UsageValue = objUsageValue
    End Sub
    Public ReadOnly Property DetectionTime As Long
    Public ReadOnly Property SolValue As Long
    Public ReadOnly Property UsageValue As Long
End Class

Get the data and transform:

'Get Solar Gen
Dim SolarResultsfound = VATWebClient.UploadString("https://XXXXXX.org/feed/data.json?id=" + SolarID + "&start=" + UNStartTime.ToString + "&end=" + UNEndTime.ToString + "&interval=1300", "")

'Get Usage
Dim UsageResultsfound = VATWebClient.UploadString("https://xxxxxx.org/feed/data.json?id=" + UsageID + "&start=" + UNStartTime.ToString + "&end=" + UNEndTime.ToString + "&interval=1300", "")

Edit: The data I get back is in the following format in both requests, up to 3k sets each.

[[1579617389000,132],[1579617399000,136],[1579617409000,139],[1579617419000,137]]

Edit: I then want to combine both (and there will be more in the future) to send to google charts. I am taking the unix time (first column) and value (second column) from the first results and only the value column from the second.

'Create an list from json results
Dim numbers As New List(Of NumbersItem)()
Dim jsonArrays = JArray.Parse(SolarResultsfound)
Dim jsonUsageArrays = JArray.Parse(UsageResultsfound)
For Each array As JArray In jsonArrays.Children()
    For Each usage As JArray In jsonUsageArrays
          numbers.Add(New NumbersItem(CType(array.First, Long), CType(array.Last, Long), CType(usage.Last, Long)))
    Next
Next

And then I try and Serialize with the following.

Dim jsonTxt As String = Newtonsoft.Json.JsonConvert.SerializeObject(numbers)

And get the following error

Unable to cast object of type 'xxxxxx.NumbersItem' to type 'System.Collections.IEnumerable'.

I have a feeling that it must be the way I am calling the serializer but I cant find the correct syntax.


Solution

  • Sorry I seem to have answered my own question. Wile adding more explination I found myself thinking if it was possible to combine multiple data sets in the same google chart and yes it is!

    Produced two datatables as below.

    //setup DT for solar production
    var dt1 = new google.visualization.DataTable();
    dt1.addColumn('number', 'Date');
    dt1.addColumn('number', 'Solar (watts)');
    dt1.addRows(@GenResults);
    
    //setup DT for usage
    var dt2 = new google.visualization.DataTable();
    dt2.addColumn('number', 'Date');
    dt2.addColumn('number', 'Usage (watts)');
    dt2.addRows(@UseResults);
    

    I then combined the two data tables into one joined view, and it works.

    var data = google.visualization.data.join(dt1, dt2, 'full', [[0, 0]], [1], [1]);
    

    Thanks, Richard.