Question : How do I get a json list into the proper VB.NET class

I've been struggling trying to get the following json string into .net classes
This is my first attempt at trying to read json strings and deserializers etc.
The string comes from the Etsy API2 where a list of listings on that site can be retrieved.
It consists of several results of listings.  Listings has many properties
An example of the full output is

 jsonstr2 = "{""count"":93,""results"":[{""listing_id"":45291928,""state"":""active"",""user_id"":8483376,""ti ...

This has many more properties which I will not bore you with here.
I know the basic json into class works because when I use the single listing string

jsonstr2 = "{""listing_id"":45291928,""state"":""active"",""user_id"  ...

and the following code

Dim EtsyListing As Listingv2
EtsyListing = JSONHelper.Deserialize(Of Listingv2)(jsonstr2)
Dim temp9 As String = Etsy.user_ID

    Public Class JSONHelper
        Public Shared Function Deserialize(Of T)(ByVal json As String) As T
            Dim serializer As New       System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(T))
            Using Stream As New MemoryStream(New UTF8Encoding().GetBytes(json))
                Return (serializer.ReadObject(Stream))
            End Using
        End Function
    End Class

which puts it into the listingv2 class everything works.

When I try to have several list by using this code:
            Dim Listings As ListingCollections
            Lisitings = JSONHelper.Deserialize(Of ListingCollections)(jsonstr)
            Dim temp9 As Integer = Listings.results.Count
            Dim temp11 As String = Listings.results(1).URL

only the first list is in the collection, even though there should be 3.  When I try to access lthese, of course it fails stating that I've exceeded the index.

The problem appears to be the listing collection class which I have as the following:

Imports System.IO
Imports System.Text
Imports System.Xml.Serialization
Imports System.Runtime.Serialization
Imports System.Collections.Generic


<DataContract()> _
Public Class ListingCollections
    Implements IEnumerable, IEnumerator

#Region "Public Properties"

    Private _Etsylistings As List(Of Listingv2)
    <DataMember(Name:="results")> _
    Public Property EtsyListings() As List(Of Listingv2)
        Get
            Return _EtsyListings
        End Get
        Set(ByVal value As List(Of Listingv2))
            _EtsyListings = value
        End Set
    End Property

    Private _totcount As Integer
    <DataMember(Name:="count")> _
    Public Property totcount() As Integer
        Get
            Return _totcount
        End Get
        Set(ByVal value As Integer)
            _totcount = value
        End Set
    End Property

End Class

The initial count in jsonstr2 is also placed correctly.  Only the multiple Etsy Listings are not retrieved correctly.  It appears that I've written the listingcollection class incorrectly.  

Any help appreciated.





Answer : How do I get a json list into the proper VB.NET class

you need to set all object or the deserialization will fail with an exception

Your last code with some update

I replaced the ArrayList by : List(Of String))
A use a full json object
renamed stats to state back
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
Imports System.IO
Imports System.Text
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Json

Module Module1

    Sub Main()
        Dim json As String = "{""count"":93,""results"":[{""listing_id"":45291928,""state"":""active"",""user_id"":8483376,""title"":"""",""description"":"""",""creation_tsz"":20100824,""ending_tsz"":20100824,""original_creation_tsz"":20100824,""last_modified_tsz"":20100824,""price"":1234.56,""currency_code"":"""",""quantity"":1000,""tags"":[],""materials"":[],""featured_rank"":null,""state_tsz"":0,""hue"":0,""saturation"":0,""brightness"":0,""is_black_and_white"":true,""urls"":"""",""views"":9876}]"
        Dim lstCollections As New ListingCollections()
        Dim ms As New MemoryStream(Encoding.UTF8.GetBytes(json))
        Dim ser As New DataContractJsonSerializer(lstCollections.GetType())
        lstCollections = TryCast(ser.ReadObject(ms), ListingCollections)
        Console.WriteLine("state value of the third json element from results is : " & lstCollections.EtsyListings.Item(0).is_black_and_white)
        ms.Close()
    End Sub


    <DataContract()> _
    Public Class ListingCollections
        Private _EtsyListings As List(Of Listingv2)
        Private _totcount As Integer

        <DataMember(Name:="results")> _
        Public Property EtsyListings() As List(Of Listingv2)
            Get
                Return _EtsyListings
            End Get
            Set(ByVal value As List(Of Listingv2))
                _EtsyListings = value
            End Set
        End Property

        <DataMember(Name:="count")> _
        Public Property totcount() As Integer
            Get
                Return _totcount
            End Get
            Set(ByVal value As Integer)
                _totcount = value
            End Set
        End Property

        Public Sub New()
        End Sub

        Public Sub New(ByVal EtsyListings As List(Of Listingv2), ByVal totcount As Integer)
            Me._EtsyListings = EtsyListings
            Me._totcount = totcount
        End Sub

    End Class

    <DataContract()> _
    Public Class Listingv2
        Private _listing_id As Integer
        Private _state As String
        Private _user_id As Integer
        Private _title As String
        Private _description As String
        Private _creation_tsz As Integer
        Private _ending_tsz As Integer
        Private _original_creation_tsz As Integer
        Private _last_modified_tsz As Integer
        Private _price As Decimal?
        Private _currency_code As String
        Private _quantity As Integer
        Private _tags As List(Of String)
        Private _materials As List(Of String)
        Private _featured_rank As Nullable(Of Integer)
        Private _state_tsz As Integer
        Private _hue As Integer
        Private _saturation As Integer
        Private _brightness As Integer
        Private _is_black_and_white As Boolean
        Private _url As String
        Private _views As Integer

        <DataMember(Name:="listing_id")> _
        Public Property listing_id() As Integer
            Get
                Return _listing_id
            End Get
            Set(ByVal value As Integer)
                _listing_id = value
            End Set
        End Property

        <DataMember(Name:="state")> _
        Public Property state() As String
            Get
                Return If(_state Is Nothing, "", _state)
            End Get
            Set(ByVal value As String)
                _state = value
            End Set
        End Property

        <DataMember(Name:="user_id")> _
        Public Property user_id() As Integer
            Get
                Return _user_id
            End Get
            Set(ByVal value As Integer)
                _user_id = value
            End Set
        End Property

        <DataMember(Name:="title")> _
        Public Property title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property

        <DataMember(Name:="description")> _
        Public Property description() As String
            Get
                Return _description
            End Get
            Set(ByVal value As String)
                _description = value
            End Set
        End Property

        <DataMember(Name:="creation_tsz")> _
        Public Property creation_tsz() As Integer
            Get
                Return _creation_tsz
            End Get
            Set(ByVal value As Integer)
                _creation_tsz = value
            End Set
        End Property

        <DataMember(Name:="ending_tsz")> _
        Public Property ending_tsz() As Integer
            Get
                Return _ending_tsz
            End Get
            Set(ByVal value As Integer)
                _ending_tsz = value
            End Set
        End Property

        <DataMember(Name:="original_creation_tsz")> _
        Public Property original_creation_tsz() As Integer
            Get
                Return _original_creation_tsz
            End Get
            Set(ByVal value As Integer)
                _original_creation_tsz = value
            End Set
        End Property

        <DataMember(Name:="last_modified_tsz")> _
        Public Property last_modified_tsz() As Integer
            Get
                Return _last_modified_tsz
            End Get
            Set(ByVal value As Integer)
                _last_modified_tsz = value
            End Set
        End Property

        <DataMember(Name:="price")> _
        Public Property price() As Decimal?
            Get
                Return If(_price Is Nothing, 0D, _price)
            End Get
            Set(ByVal value As Decimal?)
                _price = value
            End Set
        End Property

        <DataMember(Name:="currencycode")> _
        Public Property currency_code() As String
            Get
                Return If(_currency_code Is Nothing, "", _currency_code)
            End Get
            Set(ByVal value As String)
                _currency_code = value
            End Set
        End Property

        <DataMember(Name:="quantity")> _
        Public Property quantity() As Integer
            Get
                Return _quantity
            End Get
            Set(ByVal value As Integer)
                _quantity = value
            End Set
        End Property

        <DataMember(Name:="tags")> _
        Public Property Tags() As List(Of String)
            Get
                Return _tags
            End Get
            Set(ByVal value As List(Of String))
                _tags = value
            End Set

        End Property
        <DataMember(Name:="materials")> _
        Public Property materials() As List(Of String)
            Get
                Return _materials
            End Get
            Set(ByVal value As List(Of String))
                _tags = _materials
            End Set

        End Property

        <DataMember(Name:="featured_rank")> _
        Public Property featured_rank() As Nullable(Of Integer)
            Get
                Return _featured_rank
            End Get
            Set(ByVal value As Nullable(Of Integer))
                _featured_rank = value
            End Set
        End Property

        <DataMember(Name:="state_tsz")> _
        Public Property state_tsz() As Integer
            Get
                Return _state_tsz
            End Get
            Set(ByVal value As Integer)
                _state_tsz = value
            End Set
        End Property

        <DataMember(Name:="hue")> _
        Public Property hue() As Integer
            Get
                Return _hue
            End Get
            Set(ByVal value As Integer)
                _hue = value
            End Set
        End Property

        <DataMember(Name:="sauration")> _
        Public Property saturation() As Integer
            Get
                Return _saturation
            End Get
            Set(ByVal value As Integer)
                _saturation = value
            End Set
        End Property

        <DataMember(Name:="brightness")> _
        Public Property brightness() As Integer
            Get
                Return _brightness
            End Get
            Set(ByVal value As Integer)
                _brightness = value
            End Set
        End Property

        <DataMember(Name:="is_black_and_white")> _
        Public Property is_black_and_white() As Boolean
            Get
                Return _is_black_and_white
            End Get
            Set(ByVal value As Boolean)
                _is_black_and_white = value
            End Set
        End Property

        <DataMember(Name:="url")> _
        Public Property URL() As String
            Get
                Return _url
            End Get
            Set(ByVal value As String)
                _url = value
            End Set
        End Property

        <DataMember(Name:="views")> _
        Public Property views() As Integer
            Get
                Return _views
            End Get
            Set(ByVal value As Integer)
                _views = value
            End Set
        End Property

        Public Sub New()
        End Sub

        Public Sub New(ByVal listing_id As Integer, ByVal state As String, ByVal user_id As Integer, ByVal title As String, ByVal description As String, ByVal creation_tsz As Integer, ByVal ending_tsz As Integer, ByVal original_creation_tsz As Integer, ByVal last_modified_tsz As Integer, ByVal price As Decimal, ByVal currency_code As String, ByVal quantity As Integer, ByVal tags As List(Of String), ByVal materials As List(Of String), ByVal featured_rank As Nullable(Of Integer), ByVal state_tsz As Integer, ByVal hue As Integer, ByVal saturation As Integer, ByVal brightness As Integer, ByVal is_black_and_white As Boolean, ByVal url As String, ByVal views As Integer)
            Me._listing_id = listing_id
            Me._state = state
            Me._user_id = user_id
            Me._title = title
            Me.description = description
            Me._creation_tsz = creation_tsz
            Me._ending_tsz = ending_tsz
            Me._original_creation_tsz = original_creation_tsz
            Me._last_modified_tsz = last_modified_tsz
            Me._price = price
            Me._currency_code = currency_code
            Me._quantity = quantity
            Me._tags = tags
            Me._materials = materials
            Me._featured_rank = featured_rank
            Me._state_tsz = state_tsz
            Me._hue = hue
            Me._saturation = saturation
            Me._brightness = brightness
            Me._is_black_and_white = is_black_and_white
            Me._url = url
            Me._views = views

        End Sub

    End Class

End Module
Random Solutions  
 
programming4us programming4us