0

I get the json data in string:

I get error unable to convert array to classname

My json and the classes created are Code what i have done

On button click i have tried this.I unable to figure out how to do it

Dim inputUrl As String


Dim output As String
        TryExecute(Sub()
                       ' Using client = New WebClient()
                       city = HttpUtility.UrlEncode(If(city, String.Empty))
                       state = HttpUtility.UrlEncode(If(state, String.Empty))
                       zipcode = HttpUtility.UrlEncode(If(zipcode, String.Empty))
                       street = HttpUtility.UrlEncode(If(street, String.Empty))

                       inputUrl = String.Format(Url, AuthId, AuthToken) + String.Format(QueryString, street, city, state, zipcode)

                       ' myProducts(List < Product >= New JavaScriptSerializer().Deserialize < List < Project >> (objUSPSAPI.FormatJson(client.DownloadString(url__1))))
                       output = objUSPSAPI.FormatJson(wsClient.DownloadString(inputUrl))
                       Dim jss As New JavaScriptSerializer
                       Dim result = jss.Deserialize(Of components)(output)

1 Answer 1

0

At last i did it!

Json data:

[
    {
        "input_index": 0,
        "candidate_index": 0,
        "delivery_line_1": "1600 Amphitheatre Pkwy",
        "last_line": "Mountain View CA 94043-1351",
        "delivery_point_barcode": "940431351000",
        "components": {
            "primary_number": "1600",
            "street_name": "Amphitheatre",
            "street_suffix": "Pkwy",
            "city_name": "Mountain View",
            "state_abbreviation": "CA",
            "zipcode": "94043",
            "plus4_code": "1351",
            "delivery_point": "00",
            "delivery_point_check_digit": "0"
        },
        "metadata": {
            "record_type": "S",
            "zip_type": "Standard",
            "county_fips": "06085",
            "county_name": "Santa Clara",
            "carrier_route": "C909",
            "congressional_district": "18",
            "rdi": "Commercial",
            "elot_sequence": "0089",
            "elot_sort": "A",
            "latitude": 37.42202,
            "longitude": -122.08433,
            "precision": "Zip9",
            "time_zone": "Pacific",
            "utc_offset": -8.0,
            "dst": true
        },
        "analysis": {
            "dpv_match_code": "Y",
            "dpv_footnotes": "AABB",
            "dpv_cmra": "N",
            "dpv_vacant": "N",
            "active": "Y",
            "footnotes": "N#"
        }
    }
]

Add these references

1) ADD these references

Imports System.ServiceModel.Web
Imports System.Runtime.Serialization

Create the classes to support the json data
1)Create Classes to support json
2) Note the attributes used for class <DataContract()> and datatype <DataMember()>
3)The name of the property should be same as that in the json data

<DataContract()>   'class attribute
    Public Class JsonData

        Private _input_index As String
        <DataMember()>                     'datatype attribute
        Public Property input_index As String  'name of  property same as Json
            Get
                Return _input_index
            End Get
            Set(ByVal value As String)
                _input_index = value
            End Set
        End Property
        Private _candidate_index As String
        <DataMember()>
        Public Property candidate_index As String
            Get
                Return _candidate_index
            End Get
            Set(ByVal value As String)
                _candidate_index = value
            End Set
        End Property
        Private _delivery_line_1 As String
        <DataMember()>
        Public Property delivery_line_1 As String
            Get
                Return _delivery_line_1
            End Get
            Set(ByVal value As String)
                _delivery_line_1 = value
            End Set
        End Property
        Private _last_line As String
        <DataMember()>
        Public Property last_line As String
            Get
                Return _last_line
            End Get
            Set(ByVal value As String)
                _last_line = value
            End Set
        End Property
        Private _delivery_point_barcode As String
        <DataMember()>
        Public Property delivery_point_barcode As String
            Get
                Return _delivery_point_barcode
            End Get
            Set(ByVal value As String)
                _delivery_point_barcode = value
            End Set
        End Property
        Private _Componentlist As components    'the class of this list is down 
        <DataMember()>
        Public Property components As components    ' note here: name is same same as in json 
            Get
                Return _Componentlist
            End Get
            Set(ByVal value As components)
                _Componentlist = value
            End Set
        End Property
        Private _metadatalist As metadata  'the class of this list is down
        <DataMember()>
        Public Property metadata As metadata
            Get
                Return _metadatalist
            End Get
            Set(ByVal value As metadata)
                _metadatalist = value
            End Set
        End Property
        Private _anslysislist As analysis  'the class of this list is down
        <DataMember()>
        Public Property analysis As analysis
            Get
                Return _anslysislist
            End Get
            Set(ByVal value As analysis)
                _anslysislist = value
            End Set
        End Property
    End Class

    <DataContract()>
    Public Class components

        Private _primarynumber As Integer
        <DataMember()>
        Public Property primary_number As String
            Get
                Return _primarynumber
            End Get
            Set(ByVal value As String)
                _primarynumber = value
            End Set
        End Property
        Private _streetname As String
        <DataMember()>
        Public Property street_name As String
            Get
                Return _streetname
            End Get
            Set(ByVal value As String)
                _streetname = value
            End Set
        End Property
        Private _streetsufix As String
        <DataMember()>
        Public Property street_suffix As String
            Get
                Return _streetsufix
            End Get
            Set(ByVal value As String)
                _streetsufix = value
            End Set
        End Property

        Private _city_name As String
        <DataMember()>
        Public Property city_name As String
            Get
                Return _city_name
            End Get
            Set(ByVal value As String)
                _city_name = value
            End Set
        End Property
        Private _state_abbreviation As String
        <DataMember()>
        Public Property state_abbreviation As String
            Get
                Return _state_abbreviation
            End Get
            Set(ByVal value As String)
                _state_abbreviation = value
            End Set
        End Property
        Private _zipcode As String
        <DataMember()>
        Public Property zipcode As String
            Get
                Return _zipcode
            End Get
            Set(ByVal value As String)
                _zipcode = value
            End Set
        End Property
        Private _plus4_code As Integer
        <DataMember()>
        Public Property plus4_code As String
            Get
                Return _plus4_code
            End Get
            Set(ByVal value As String)
                _plus4_code = value
            End Set
        End Property
        Private _delivery_point As Integer
        <DataMember()>
        Public Property delivery_point As String
            Get
                Return _delivery_point
            End Get
            Set(ByVal value As String)
                _delivery_point = value
            End Set
        End Property
        Private _delivery_point_check_digit As Integer
        <DataMember()>
        Public Property delivery_point_check_digit As String
            Get
                Return _delivery_point_check_digit
            End Get
            Set(ByVal value As String)
                _delivery_point_check_digit = value
            End Set
        End Property
    End Class

    <DataContract()>
    Public Class metadata
        Private _record_type As String
        <DataMember()>
        Public Property record_type As String
            Get
                Return _record_type
            End Get
            Set(ByVal value As String)
                _record_type = value
            End Set
        End Property


        Private _zip_type As String
        <DataMember()>
        Public Property zip_type As String
            Get
                Return _zip_type
            End Get
            Set(ByVal value As String)
                _zip_type = value
            End Set
        End Property
        Private _county_fips As String
        <DataMember()>
        Public Property county_fips As String
            Get
                Return _county_fips
            End Get
            Set(ByVal value As String)
                _county_fips = value
            End Set
        End Property
        Private _county_name As String
        <DataMember()>
        Public Property county_name As String
            Get
                Return _county_name
            End Get
            Set(ByVal value As String)
                _county_name = value
            End Set
        End Property
        Private _carrier_route As String
        <DataMember()>
        Public Property carrier_route As String
            Get
                Return _carrier_route
            End Get
            Set(ByVal value As String)
                _carrier_route = value
            End Set
        End Property
        Private _congressional_district As String
        <DataMember()>
        Public Property congressional_district As String
            Get
                Return _congressional_district
            End Get
            Set(ByVal value As String)
                _congressional_district = value
            End Set
        End Property
        Private _rdi As String
        <DataMember()>
        Public Property rdi As String
            Get
                Return _rdi
            End Get
            Set(ByVal value As String)
                _rdi = value
            End Set
        End Property
        Private _elot_sequence As String
        <DataMember()>
        Public Property elot_sequence As String
            Get
                Return _elot_sequence
            End Get
            Set(ByVal value As String)
                _elot_sequence = value
            End Set
        End Property
        Private _elot_sort As String
        <DataMember()>
        Public Property elot_sort As String
            Get
                Return _elot_sort
            End Get
            Set(ByVal value As String)
                _elot_sort = value
            End Set
        End Property

        Private _latitude As String
        <DataMember()>
        Public Property latitude As String
            Get
                Return _latitude
            End Get
            Set(ByVal value As String)
                _latitude = value
            End Set
        End Property
        Private _longitude As String
        <DataMember()>
        Public Property longitude As String
            Get
                Return _longitude
            End Get
            Set(ByVal value As String)
                _longitude = value
            End Set
        End Property
        Private _precision As String
        <DataMember()>
        Public Property precision As String
            Get
                Return _precision
            End Get
            Set(ByVal value As String)
                _precision = value
            End Set
        End Property
        Private _time_zone As String
        <DataMember()>
        Public Property time_zone As String
            Get
                Return _time_zone
            End Get
            Set(ByVal value As String)
                _time_zone = value
            End Set
        End Property
        Private _utc_offset As String
        <DataMember()>
        Public Property utc_offset As String
            Get
                Return _utc_offset
            End Get
            Set(ByVal value As String)
                _utc_offset = value
            End Set
        End Property
        Private _dst As String
        <DataMember()>
        Public Property dst As String
            Get
                Return _dst
            End Get
            Set(ByVal value As String)
                _dst = value
            End Set
        End Property
    End Class

    <DataContract()>
    Public Class analysis
        Private _dpv_match_code As String
        <DataMember()>
        Public Property dpv_match_code As String
            Get
                Return _dpv_match_code
            End Get
            Set(ByVal value As String)
                _dpv_match_code = value
            End Set
        End Property
        Private _dpv_footnotes As String
        <DataMember()>
        Public Property dpv_footnotes As String
            Get
                Return _dpv_footnotes
            End Get
            Set(ByVal value As String)
                _dpv_footnotes = value
            End Set
        End Property
        Private _dpv_cmra As String
        <DataMember()>
        Public Property dpv_cmra As String
            Get
                Return _dpv_cmra
            End Get
            Set(ByVal value As String)
                _dpv_cmra = value
            End Set
        End Property
        Private _dpv_vacant As String
        <DataMember()>
        Public Property dpv_vacant As String
            Get
                Return _dpv_vacant
            End Get
            Set(ByVal value As String)
                _dpv_vacant = value
            End Set
        End Property
        Private _active As String
        <DataMember()>
        Public Property active As String
            Get
                Return _active
            End Get
            Set(ByVal value As String)
                _active = value
            End Set
        End Property
        Private _footnotes As String
        <DataMember()>
        Public Property footnotes As String
            Get
                Return _footnotes
            End Get
            Set(ByVal value As String)
                _footnotes = value
            End Set
        End Property

Get the values from the json String:

Dim output As String
output = objUSPSAPI.FormatJson(wsClient.DownloadString(inputUrl))  ' download json string from external source
Dim JsonDataobj = New List(Of JsonData)
Dim ser = New DataContractJsonSerializer(GetType(List(Of JsonData)))
Dim stream = New MemoryStream(Encoding.UTF8.GetBytes(output))
JsonDataobj = TryCast(ser.ReadObject(stream), List(Of JsonData))
If JsonDataobj IsNot Nothing AndAlso JsonDataobj.Count > 0 Then
    Dim Uspsdata As New DataTable
    Uspsdata.Columns.Add("primary_number", GetType(System.String))
    Uspsdata.Columns.Add("street_name", GetType(System.String))
    Uspsdata.Columns.Add("street_suffix", GetType(System.String))
    Uspsdata.Columns.Add("city_name", GetType(System.String))
    Uspsdata.Columns.Add("state_abbreviation", GetType(System.String))
    Uspsdata.Columns.Add("zipcode", GetType(System.String))
    Uspsdata.Columns.Add("plus4_code", GetType(System.String))
    Uspsdata.Columns.Add("delivery_point", GetType(System.String))
    Uspsdata.Columns.Add("delivery_point_check_digit", GetType(System.String))
    Dim dr As DataRow
    For Each objaddress As JsonData In JsonDataobj
        If Not objaddress.components Is Nothing Then
            dr = Uspsdata.NewRow
            dr("primary_number") = objaddress.components.primary_number.ToString()
            dr("street_name") = objaddress.components.street_name.ToString()
            dr("street_suffix") = objaddress.components.street_suffix.ToString()
            dr("city_name") = objaddress.components.city_name.ToString()
            dr("state_abbreviation") = objaddress.components.state_abbreviation.ToString()
            dr("zipcode") = objaddress.components.zipcode.ToString()
            dr("plus4_code") = objaddress.components.plus4_code.ToString()
            dr("delivery_point") = objaddress.components.delivery_point.ToString()
            dr("delivery_point_check_digit") = objaddress.components.delivery_point_check_digit.ToString()
            Uspsdata.Rows.Add(dr)
        End If
    Next
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.