Developer1903
Developer1903

Reputation: 127

Read array of objects in JSON

I have an array with objects, and I need to parse each object.

Example of what I have:

[
  {
    "object": "transaction",
    "status": "waiting_payment",
    "amount": 1
  },
  {
    "object": "transaction",
    "status": "paid",
    "amount": 2
  }
]

The code I have:

Try

  System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
  Dim Request As WebRequest = WebRequest.Create(url)
  Request.Credentials = CredentialCache.DefaultCredentials

  Using responseStream = Request.GetResponse.GetResponseStream
      Using reader As New StreamReader(responseStream)

          Dim objResponse As Object = reader.ReadToEnd()

          Dim transactionResponse = JsonConvert.DeserializeObject(Of Transaction)(objResponse.ToString())

      End Using
  End Using

Catch ex As WebException

End Try



Private Class Transaction
     Public object As String
     Public status As String
     Public amount As Integer
End Class

Then, I want to access, for example, the "amount". But I am not able to access transactionResponse.amount. This code works perfectly if I have just one object, without the "[".

How could I read each object?

Upvotes: 2

Views: 436

Answers (2)

Anu6is
Anu6is

Reputation: 2658

Only two minor changes needed to the current code (see inline comments).

Try

  System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
  Dim Request As WebRequest = WebRequest.Create(url)
  Request.Credentials = CredentialCache.DefaultCredentials

  Using responseStream = Request.GetResponse.GetResponseStream
      Using reader As New StreamReader(responseStream)

          Dim objResponse As String = reader.ReadToEnd()
          '(1) You need to Deserialize into a list (array) as your json actually represents an array
          Dim transactionResponse = JsonConvert.DeserializeObject(Of List(Of Transaction))(objResponse)

      End Using
  End Using

Catch ex As WebException

End Try



Private Class Transaction
     Public [object] As String '(2) Wrap the object in [] as Object is a reserved word
     Public status As String
     Public amount As Integer
End Class

Upvotes: 2

EmanuelF
EmanuelF

Reputation: 151

Try to use the code below:

Dim strJsonArray As String = "[{""name"":""jack""},{""name"":""john""},{""name"":""joe""}]"

Dim strJsonObject As String = "{""pets"":[{""name"":""jack""},{""name"":""john""},{""name"":""joe""}]}"

'  A JSON array must be loaded using JsonArray:
Dim jsonArray As New Chilkat.JsonArray
jsonArray.Load(strJsonArray)

'  Examine the values:
Dim i As Integer = 0
While i < jsonArray.Size
    Dim jsonObj As Chilkat.JsonObject = jsonArray.ObjectAt(i)
    Console.WriteLine(i & ": " & jsonObj.StringOf("name"))

    i = i + 1
End While

'  Output is:

'   0: jack
'   1: john
'   2: joe


'  A JSON object must be loaded using JsonObject
Dim jsonObject As New Chilkat.JsonObject
jsonObject.Load(strJsonObject)

'  Examine the values:
i = 0
Dim numPets As Integer = jsonObject.SizeOfArray("pets")
While i < numPets
    jsonObject.I = i
    Console.WriteLine(i & ": " & jsonObject.StringOf("pets[i].name"))
    i = i + 1
End While

'  Output is:

'   0: jack
'   1: john
'   2: joe

For more information about the code, access HERE

Upvotes: 1

Related Questions