Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Parsing JSON with VB.NET and Newtonsoft.Json [HELP PLS]

I'm getting back into programming after several years of being out of it (basically a skid all over again fml)

Title says it all really, I'm trying to parse JSON data but I keep getting errors and I'm not sure where I'm going wrong. Any help would be much appreciated. It's grabbing the data but erroring when trying to parse

I know I'm probably being extremely stupid somewhere but I just can't figure out what's going wrong for the life of me

Code
Imports System.IO
Imports System.Net
Imports System.Xml
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader

        Try

            request = DirectCast(WebRequest.Create("http://api.com/json/" + txtAPI.Text), HttpWebRequest)
            response = DirectCast(request.GetResponse(), HttpWebResponse)
            reader = New StreamReader(response.GetResponseStream())

            Dim rawresp As String
            rawresp = reader.ReadToEnd()

            Dim json As String = rawresp
            Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(json)
            Dim jsonArray As JArray = jsonObject("status")

            For Each item As JObject In jsonArray
                txtCountry.Text = item.SelectToken("country").ToString
            Next

        Catch ex As Exception
            MsgBox(ex.ToString)
            Clipboard.SetText(ex.ToString)
        Finally
            If Not response Is Nothing Then response.Close()
        End Try
    End Sub
End Class

I get the following error:

System.InvalidCastException: Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JArray'.
at Form1.Button1_Click(Object sender, EventArgs e) in \Form1.vb:line 28
It would be helpful if you posted the actual JSON payload that you are trying to parse :)
My guess would be that since you are iterating over the jsonArray and trying to get the token "test", that not every item in this array has such a token called "test", so calling the .ToString method on it would give you a System.NullReferenceException
(Nov 25, 2020 10:58 AM)neon_light Wrote:  It would be helpful if you posted the actual JSON payload that you are trying to parse :)
My guess would be that since you are iterating over the jsonArray and trying to get the token "test", that not every item in this array has such a token called "test", so calling the .ToString method on it would give you a System.NullReferenceException

I'm no longer getting the NullReference error, but I am now getting this one >.>

System.InvalidCastException: Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JArray'.
at Form1.Button1_Click(Object sender, EventArgs e) in \Form1.vb:line 28

PM for the JSON payload
(Nov 25, 2020 11:17 AM)outlaw Wrote:  I'm no longer getting the NullReference _error_, but I am now getting this one >.>

System.InvalidCastException: Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JArray'.
  at Form1.Button1_Click(Object sender, EventArgs e) in \Form1.vb:line 28

PM for the JSON payload


My recommendation is to just parse the JSON string into an object.

So after fetching the JSON payload from the server, you just parse it into your vb.net object like so:

Code
Dim ob As Results= JsonConvert.DeserializeObject(Of Results)(your_json_string)

After that, you can easily access all values via the class.

Here is a website that can help you with generating the "Results" class:

httpx://jsonutils.com/ (<-- Remove the extra x, i cant post links yet)

Just paste in your json string, select vb.net as language and click generate.
(Nov 25, 2020 11:24 AM)neon_light Wrote:  My recommendation is to just parse the JSON string into an object.

So after fetching the JSON payload from the server, you just parse it into your vb.net object like so:

Code
Dim ob As Results= JsonConvert.DeserializeObject(Of Results)(your_json_string)

After that, you can easily access all values via the class.

Here is a website that can help you with generating the "Results" class:

httpx://jsonutils.com/  (<-- Remove the extra x, i cant post links yet)

Just paste in your json string, select vb.net as language and click generate.

Do you have Discord dude? I think I know where I'm going wrong
(Nov 25, 2020 11:32 AM)outlaw Wrote:  Do you have Discord dude? I think I know where I'm going wrong

Yes sure. its: neon_light#9498
(Nov 25, 2020 11:35 AM)neon_light Wrote:  Yes sure. its: neon_light#9498

Added you :)


Problem solved thanks to neon_light! The guy knows his shit :)
(Nov 25, 2020 11:42 AM)outlaw Wrote:  Added you :)


Problem solved thanks to neon_light! The guy knows his shit :)

It would be a good not to drag the discussion off the hf forum to discord next time,because thus knowledge is not open all.In any case, you could find a solution for your problem on the forum because there were users with similar problems like your.Anyway, I'm glad you solved the problem and continue with hard work.
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Nov 27, 2020 10:42 AM)itcorp Wrote:  It would be a good not to drag the discussion off the hf forum to discord next time,because thus knowledge is not open all.In any case, you could find a solution for your problem on the forum because there were users with similar problems like your.Anyway, I'm glad you solved the problem and continue with hard work.

I actually ended up abandoning VB.NET after realizing how dead it is now. No real support for any of the latest tech. I've now moved over to C# and got everything working perfectly just how I wanted and with relative ease.
(Nov 27, 2020 10:44 AM)outlaw Wrote:  I actually ended up abandoning VB.NET after realizing how dead it is now. No real support for any of the latest tech. I've now moved over to C# and got everything working perfectly just how I wanted and with relative ease.

You made a smart move buddy, It is good to follow the technology considering the moves of Microsoft regarding that field.I'm happy for you,that you achieved what you wanted.
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Nov 27, 2020 10:44 AM)outlaw Wrote:  I actually ended up abandoning VB.NET after realizing how dead it is now. No real support for any of the latest tech. I've now moved over to C# and got everything working perfectly just how I wanted and with relative ease.

As long as .net framework is alive, VB.NET is alive.

Anyway, you should have gone with serialization and deserialization with Newtonsoft.
https://www.newtonsoft.com/json/help/htm...ngJSON.htm

That way, you could use those JSON as an object pretty neatly.
Same approach if you're on VB.NET or C#

Regards,
Rmdy
(Nov 27, 2020 06:48 PM)Rmdy Wrote:  As long as .net framework is alive, VB.NET is alive.

Anyway, you should have gone with serialization and deserialization with Newtonsoft.
https://www.newtonsoft.com/json/help/htm...ngJSON.htm

That way, you could use those JSON as an object pretty neatly.
Same approach if you're on VB.NET or C#

Regards,
Rmdy

VB was giving me issues, even using Newtsoft.Json, moved over to c# and I've had no issues other than ones I caused myself.

VB.NET always been ass. The only reason I ever went there in the first place was to port from VB6 after it became unsupported.
(Nov 27, 2020 11:38 PM)outlaw Wrote:  VB was giving me issues, even using Newtsoft.Json, moved over to c# and I've had no issues other than ones I caused myself.

VB.NET always been ass. The only reason I ever went there in the first place was to port from VB6 after it became unsupported.

I love to know those issue and have some discussion but that would be for another thread.
Glad that your issue had already resolve.

Regards,
Rmdy


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by roosiinfidel09May 26, 2021 06:32 AM 1 274 May 26, 2021 06:32 AM
Last Post: h0lyl337
[C#] by DeadShot32Sep 15, 2019 02:36 AM 3 188 Sep 15, 2019 02:36 AM
Last Post: riu41776
[C] by int main()Feb 4, 2019 11:03 PM 3 205 Feb 4, 2019 11:03 PM
Last Post: int main()