Ke Vin
Ke Vin

Reputation: 3750

How to parse JSON string properly in Vb.net using Newtonsoft

How to parse JSON String to Object in vb.net properly? since my JSON data have [] this bracket, i think it's count as array right? i can't change my JSON data since i have another apps consume that JSON.

Here is how my JSON looked like :

[
  {
    "Id": "WEB1853257",
    "Tgl_OPJ": "2018-10-31T08:32:14.223Z",
    "Tgl_Jth_Tempo": "2018-11-07T08:25:14.436Z",
    "Nm_Plg": "PURWACOKRO",
    "Nm_Slsman": "BURHAN",
    "Ket": "BARU",
    "Divisi": "IPHONE X"
  },
  {
    "Id": "WEB1853240",
    "Tgl_OPJ": "2018-10-30T16:41:30.393Z",
    "Tgl_Jth_Tempo": "2018-11-06T23:59:00.000Z",
    "Nm_Plg": "PURWACOKRO",
    "Nm_Slsman": "BURHAN",
    "Ket": "SEKEN",
    "Divisi": "IPHONE X"
  },
  {
    "Id": "WEB1853238",
    "Tgl_OPJ": "2018-10-30T16:28:43.416Z",
    "Tgl_Jth_Tempo": "2018-11-06T23:59:00.000Z",
    "Nm_Plg": "PURWACOKRO",
    "Nm_Slsman": "BURHAN",
    "Ket": "SEKEN",
    "Divisi": "IPHONE XS"
  }
]

and here is my vb.net code :

Imports System.Net
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim url As String = String.Format("http://localhost:5000/phoneOrderList")

        Dim serviceRequest As New WebClient
        Dim response As String = serviceRequest.DownloadString(New Uri(url))

        Dim a As PhoneOrderList = JsonConvert.DeserializeObject(Of PhoneOrderList)(response)
        MessageBox.Show(a.Nm_Plg)

    End Sub
End Class

Public Class PhoneOrderList
    Public Property Id As String
    Public Property Tgl_OPJ As String
    Public Property Tgl_Jth_Tempo As String
    Public Property Nm_Plg As String
    Public Property Nm_Slsman As String
    Public Property Ket As String
    Public Property Divisi As String
End Class

when i click the button it give me a error like this :

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'WindowsApp2.OPJList' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.'

i already change it to array type as list, anything i missed?

Upvotes: 1

Views: 3902

Answers (1)

theduck
theduck

Reputation: 2617

You need to convert your JSON into a List of PhoneOrderList rather than a single PhoneOrderList object. So ...

Dim a = JsonConvert.DeserializeObject(Of List(Of PhoneOrderList))(response)

Will correctly convert your JSON into a list of PhoneOrderList objects. With the JSON above you should then see a.Count equals 3, a(0).Id equals "WEB1853257", a(1).Id equals "WEB1853240" etc.

Upvotes: 1

Related Questions