How to parse this JSON string properly - vb.net

I have some API That returns the following JASON, and im trying to parse it to be able to pull from any attributes i want.
JSON im trying to parse: https://pastebin.com/i06GRD0f
I'm assuming the proper way would be to load everything into an array but i can't get it to work right as it's a little confusing with all different parameters.
Imports System.Web.Script.Serialization
Public Class ServerAttributes
Public Property assists() As String
Public Property bestRankPoint() As String
Public Property boosts() As String
Public Property dBNOs() As String
Public Property dailyKills() As String
Public Property dailyWins() As String
Public Property damageDealt() As String
Public Property days() As String
Public Property headshotKills() As String
Public Property heals() As String
Public Property killPoints() As String
Public Property kills() As String
Public Property longestKill() As String
Public Property longestTimeSurvived() As String
Public Property losses() As String
Public Property maxKillStreaks() As String
Public Property mostSurvivalTime() As String
Public Property rankPoints() As String
Public Property rankPointsTitle() As String
Public Property revives() As String
Public Property rideDistance() As String
Public Property roadKills() As String
Public Property roundMostKills() As String
Public Property roundsPlayed() As String
Public Property suicides() As String
Public Property swimDistance() As String
Public Property teamKills() As String
Public Property timeSurvived() As String
Public Property top10s() As String
Public Property vehicleDestroys() As String
Public Property walkDistance() As String
Public Property weaponsAcquired() As String
Public Property weeklyKills() As String
Public Property weeklyWins() As String
Public Property winPoints() As String
Public Property wins() As String
End Class
Public Class ServerData
Public Property data() As ServerAttributes
End Class
Module StaterMainClass
Public Sub GetInfo()
Dim jss As New JavaScriptSerializer()
Dim DataResponse As ServerData = jss.Deserialize(Of ServerData)(JSON)
Debug.Print(DataResponse.data.assists(0))
End Sub
End Module

Related

Unable to cast object of type 'SubmitPO.VARCOMMERCE.PO.LineRequest[]' to type 'System.String[] using AddObject in RestSharp

When I try to create a RestSharp Request by adding an object I get this error: (It happens on request.AddObject(po))
Unable to cast object of type 'SubmitPO.VARCOMMERCE.PO.LineRequest[]' to type 'System.String[]
My code is like such:
'''
Public Function submitPO(ByRef po As OrderRequestJson, ByVal test As Boolean) As IRestResponse
'get bearer token
Dim client = New RestClient("https://api.ingrammicro.com:443/sandbox/resellers/v6/orders")
Dim request As RestRequest = New RestRequest() With {
.Method = Method.POST
}
request.AddHeader("Content-Type", "application/x-www-form-urlencoded")
request.AddHeader("authorization", "Bearer " + Me.BearerAccessToken)
request.AddHeader("Accept", "application/json")
request.AddHeader("IM-CustomerNumber", Me.AccountNumber)
request.AddHeader("IM-CountryCode", "US")
request.AddHeader("IM-CorrelationID", Guid.NewGuid.ToString())
request.AddObject(po)
Dim response = client.Execute(request)
End Function
'''
And the model is as such
'''
#Region "Request"
Public Class OrderRequestJson
Public Property customerOrderNumber As String
Public Property endCustomerOrderNumber As String
Public Property notes As String
Public Property acceptBackOrder As Boolean
Public Property shipToInfo As Shiptoinfo
Public Property lines As LineRequest()
Public Property shipmentDetails As ShipmentDetails
Public Property additionalAttributes As Additionalattribute()
End Class
Public Class Shiptoinfo
Public Property contact As String
Public Property companyName As String
Public Property addressLine1 As String
Public Property addressLine2 As String
Public Property city As String
Public Property state As String
Public Property postalCode As String
Public Property countryCode As String
Public Property phoneNumber As String
Public Property email As String
End Class
Public Class LineRequest
Public Property customerLineNumber As String
Public Property ingramPartNumber As String
Public Property quantity As Integer
End Class
Public Class Additionalattribute
Public Property attributeName As String
Public Property attributeValue As String
End Class
Public Class ShipmentDetails
Public Property carrierCode As String
Public Property freightAccountNumber As String
Public Property shipComplete As String
Public Property requestedDeliveryDate As String
Public Property signatureRequired As Boolean
Public Property shippingInstructions As String
End Class
#End Region
'''

The entity type is not part of the model for the current context - DBFirst

I have a query which selects a list of subrep. The query looks like this:
Public Async Function ReturnSubRepsAsync(user As BaseUser) As Task(Of IList(Of SubRep)) Implements IGeneralAccountManagementRepository.ReturnSubRepsAsync
'The entity type SubRep is not part of the model for the current context.
Using webDataDBContext As New WebDataEntities()
'So here it just has an SQL query which it will execute when requested
Dim subReps = webDataDBContext.Subreps.Where(Function(x) x.CID = user.CID)
If (String.IsNullOrEmpty(user.Password) = False) Then
'Add another where onto the SQL query
subReps = subReps.Where(Function(x) x.HASH_PASSWORD = user.PasswordHash)
End If
If (String.IsNullOrEmpty(user.Username) = False) Then
'Add another where onto the SQL query
subReps = subReps.Where(Function(x) x.username = user.Username)
End If
'Execute the query
Return Await subReps.ToListAsync()
End Using
End Function
Now due to entity framework database not including validation such as MaxLength on strings, I decided to follow this tutorial: https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/enhancing-data-validation
And add another partial public class which uses a MetaDataType to implement the validation. Now during debugging, I thought it was an issue with a column so I decided to comment out all of the code inside my separate partial class to test if it works, so I'm not left with an empty husk of a partial class. It has not made a difference, it still errors on the first where of my query.
My entity framework generated class looks like this:
Partial Public Class Subrep
Public Property id As Integer
Public Property CID As String
Public Property username As String
Public Property LastName As String
Public Property FirstName As String
Public Property MIddleInitial As String
Public Property Address As String
Public Property Address2 As String
Public Property City As String
Public Property State As String
Public Property Zip As String
Public Property HomePhone As String
Public Property EmailAddress As String
Public Property InActive As String
Public Property CellPhone As String
Public Property StartDate As String
Public Property AuditName As String
Public Property Background As String
Public Property FDCPA As String
Public Property Choicepoint As String
Public Property Badge As String
Public Property ConfAgree As String
Public Property NCCI_OK As String
Public Property InactiveRepNo As String
Public Property Approved As String
Public Property DateApproved As String
Public Property Denied As String
Public Property DateDenied As String
Public Property DeniedNotes As String
Public Property DOB As String
Public Property SSN As String
Public Property MegansLaw As String
Public Property MASTER As String
Public Property VACATION As String
Public Property FC_DEFAULT_FEE As Nullable(Of Decimal)
Public Property LM_DEFAULT_FEE As Nullable(Of Decimal)
Public Property MAS90 As String
Public Property SOS As String
Public Property HASH_PASSWORD As String
Public Property changepass As String
Public Property NOTIFICATION_OFF As String
Public Property Capacity As String
Public Property internalqc As String
Public Property New_Rep As String
Public Property Stand_By As String
Public Property Vacation_Start_date As Nullable(Of Date)
Public Property Vacation_End_date As Nullable(Of Date)
Public Property sub_coreIP As String
Public Property rating As String
Public Property date_last_updated As Nullable(Of Date)
End Class
And my separate partial class which is supposed to be used for validation looks like this:
Partial Public Class SubRep
End Class
So even though it's empty I get the error listed in the title. I have gotten this to work with multiple entities perfectly fine.
Just wow.
So with visual studio intellisense if you press F12 to try to find references it highlighted both partial classes Subrep and SubRep so that would lead you to believe that both were associated with one another. Turns out that because of the uppercase R in the second partial class SubRep, it's not associated with the model and therefore needs to match the case of the entity Subrep. Like this:
Partial Public Class Subrep
End Class
What's even weirder is that the Property in my DBContext uses an uppercase SubRep. Like this:
Public Overridable Property Subreps() As DbSet(Of SubRep)

Assigning values to an object and its nested object

I am a novice and am missing something simple. I have two Classes
Public Class Param
Public Property temperature As String
Public Property display As Boolean
Public Property storage As Boolean
Public Property reason As Boolean
Public Property stats As Object
Public Property errors As Object
End Class
Public Class getTemperature
Public Property method As String
Public Property params As Param()
Public Property id As String
End Class
I want to declare and assign values to the objects but I keep getting the error "Object reference not set to an instance of an object" when trying to assign values to items within param. I don't understand, I have created both the object GetTemperature and the object Params, what am I missing?
Dim GetTemp As New getTemperature
GetTemp.method = TextBoxMethod.Text
GetTemp.id = TextBoxID.Text
Dim params As New Param
params.temperature = "true"
'GetTemp.params(0) = params
I have also tried, but get the same response:
Dim GetTemp As New getTemperature
GetTemp.method = TextBoxMethod.Text
GetTemp.id = TextBoxID.Text
GetTemp.params(0).temperature = "True"
Try this:
Public Class Param
Public Property temperature As String
Public Property display As Boolean
Public Property storage As Boolean
Public Property reason As Boolean
Public Property stats As Object
Public Property errors As Object
End Class
Public Class getTemperature
Public Property method As String
Public Property params As List(Of Param)
Public Property id As String
Public Sub New()
params = New List(Of Param)
End Sub
End Class
So you could write something like:
Dim a As New getTemperature
a.params.Add(New Param)
It will work if you remove the parenthesis
Public Class getTemperature
Public Property method As String
Public Property params As Param
Public Property id As String
End Class
and then
Dim GetTemp As New getTemperature
GetTemp.method = TextBoxMethod.Text
GetTemp.id = TextBoxID.Text
Dim params As New Param
params.temperature = "true"
GetTemp.params = params

DropDownListFor using List of Class

I cant seem to get my HTML.DropDownListFor() to work properly with my structures.
CategoriesEditViewModel
Public Class CategoriesEditViewModel
Public Property Categories As List(Of cihCategoryOrgDef)
Public Property Category As cihCategoryOrgDef
Public Property lists As cihLists = New cihLists()
Public Property SelectedItem As String
Public Sub New(catId as Guid)
SelectedItem = codId.ToString
lists.loadClubWaivers(ZenCommon.CurrentOrgId)
End Sub
End Class
cihLists
Private orgWaiverList As List(of cihWaiver)
Public Sub loadClubWaivers(orgId as Guid)
'Go to database and populate orgWaiverList
End Sub
Public ReadOnly Property organizationClubWaivers() As List(Of cihWaiver)
Get
Return orgWaiverList
End Get
End Property
cihWaiver
Public Class cihWaiver
Public waiverId As Guid = Guid.Empty
Public waiverName As String = ""
Public waiverText As String = ""
End Class
Edit View Page
#Html.DropDownListFor(Function(m) m.SelectedItem, New SelectList(Model.lists.organizationClubWaivers, "waiverId", "waiverText", Model.lists.organizationClubWaivers))
The error i get is 'Library.cihWaiver' does not contain a property with the name 'waiverId'.
but the cihWaiver class clearly has an item for 'waiverId'. I haven't done MVC stuff in a while, so maybe i'm going about this all wrong.
The answer was as simple as changing my cihWaiver Class to this:
Public Class cihWaiver
Public Property waiverId As Guid = Guid.Empty
Public Property waiverName As String = ""
Public waiverText As String = ""
End Class
cane someone explain to me why the keyword Property is so important?

How to iterate through every property in an every Object in my list of Objects vb

So I have a list of Objects and I want to get every property in every object and save it as a string.
Object looks like
Public Class parameters
Public Property test As String
Public Property test_type As String
Public Property user_test_name As String
Public Property meas As String
Public Property spec_min As String
Public Property spec_max As String
Public Property spec_unit As String
Public Overrides Function ToString() As String
Return user_test_name
End Function
End Class
This is what i have so far
For Each parameters In param
Dim myType As Type = GetType(parameters)
Dim properties As System.Reflection.PropertyInfo() = myType.GetProperties()
For Each p As System.Reflection.PropertyInfo In properties
MsgBox(p.Name)
Next
Next