Leveraging the power of VB.net to extract data from JSON code

Hello, I need to extract the values of 21, black jack, and tongits including their scores from the following JSON code:

{
  "id": 1,
  "status": "PARTIAL",
  "scores": [
    {
      "scoreCard": {
        "21": 0,
        "black jack": 0,
        "tongits": 0
      },
      "question": "Favorite game",
      "questionnaireId": 1
    },
    {
      "scoreCard": {
        "red": 0,
        "green": 0,
       "blue": 0,
        "black": 0
      },
      "question": "Favorite color",
      "questionnaireId": 2
    }
  ]
}

Below is my VB code that currently only retrieves the values for Favorite game and Favorite color:

Dim webC As New System.Net.WebClient
     Dim json As String =webC.DownloadString("http://localhost:8080/report/survey/1/result")

    Dim o As JObject = JObject.Parse(json)

    Dim results As List(Of JToken) = o.Children().ToList

    For Each item As JProperty In results
        item.CreateReader()
        Select Case item.Name

            Case "scores"

                Dim question As String

                For Each subitem As JObject In item.Values
                    question = subitem("question")
                    Listview2.Items.Add(question)

                Next
        End Select
    Next

Answer №1

If you're looking to extract specific data points, it's more efficient to narrow down your search:

Dim jstr = ...from any source...

Dim jData = JObject.Parse(jstr)

Console.WriteLine(jData("scores")(0)("scoreCard")("21"))
Console.WriteLine(jData("scores")(0)("scoreCard")("black jack"))
Console.WriteLine(jData("scores")(0)("scoreCard")("tongits"))

Console.WriteLine(jData("scores")(0)("question"))
Console.WriteLine(jData("scores")(1)("question"))

scores is an array enclosed in [ ... ], so make sure to index it properly or use your IDE's auto-complete feature to access the desired information. Here are the results:

0
0
0
Favorite game
Favorite color

However, if you need to retrieve at least 4 specific values - such as the 2 questions and 3 scores - consider deserializing the data into a class object for easier navigation through properties.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Properties of jQuery UI events and the ui object are important for manipulating user

While working with the jQuery UI framework for Interactions, you can utilize custom functions that involve two parameters known as 'event' and 'ui'. I have been trying to discover the methods and properties associated with these paramet ...

Issue with JQuery time picker functionality not functioning properly upon repeat usage

I am facing an issue with a modal dialog that contains a form loaded via ajax. The form includes a time field populated using the jquery timepicker. Everything works perfectly when I open the dialog for the first time. However, if I try to load the dialog ...

Hover effect for changing image source not functioning as anticipated

I'm having issues creating an image that plays a GIF on hover. I came across some jQuery code on this website that should work, but it's not working for me. Any thoughts on what the problem might be? Here is the HTML and JavaScript code: $(doc ...

Update the input value following a successful action

How can I update the value of an input field after a successful ajax call? I have tried the following approach but it doesn't seem to be working. The result from the alert is 80000 Here is the HTML input code: <input class="form-control" type=" ...

Bug in RestEasy where Jettison is not correctly handling single element arrays

Problem description: RestEasy with Jettison When the array contains two elements, the format is: {"MyArray" : {"Array" : [{"a" : 1, "b" : 2}, {"a" : 3, "b" : 4}]}} However, when the array has a single element, the format is: {"MyArray" : {"Array" : {"a ...

Objective C "callback function"

As a beginner in Objective C, I am currently working on developing my new app and have some queries regarding callback functions. Here is the scenario: I have an NSObject class named Person which represents a person. This class has parameters such as the ...

Challenges faced when developing a chat application using node.js and socket.io

I was watching a tutorial on YouTube about creating a basic chat app, and I ran into an issue when trying to run the "npm install" command. The error message I received is shown in the following image: enter image description here Despite following the s ...

Glistening tabPanel and Analytics by Google

I recently completed a comprehensive tutorial here that delves into the intricacies of Google Analytics. Despite grasping the concepts explained in the tutorial, my understanding of jQuery remains at ground zero. In my ShinyApp, I utilize multiple tabPanel ...

Which is more efficient in jQuery for selecting the nth sibling of a node: chaining .next() n times or using .nextAll(':eq(n-1)')?

If I am looking to select the nth sibling of a node, with an arbitrary number of siblings present, should I use multiple calls of .next() or just one call of .nextAll(':eq(n-1)')? It seems like using multiple .next() calls could result in extra ...

Adjusting the X-axis in Highstock: Tips and Tricks

Is there a way to adjust the X axis on this chart? My goal is to shift it so that it only covers half of the width, leaving the other half blank for future plotlines. Any suggestions on how to achieve this? Thanks! :) ...

Does Python for Windows come with the json package?

Does Python for Windows come pre-installed with the json package? ...

Error in API function call with AngularJS: Reference Issue

formApp.controller('load', function ($scope, ApiCall, $window, $http) { $window.onload = function () { alert("the page loaded and will now call the function"); ApiCall.GetApiCall("signOn", "GetSingleSignOn").success(function (data) { ...

Combining PHP JQuery Dialog with Datatables results in a sleek design loss

Recently, I encountered an issue with my code. I have a file called incident_view.php which pulls data from the database and displays it using Datatables. Everything was working fine until I tried to open this page in a JQuery dialog on another page called ...

example of using relative jquery countdown.js

I've been attempting to grasp JavaScript and incorporate the countdown found at this link (specifically, the example with a 300-second countdown), but after spending a few hours on it, I haven't been able to get it functioning properly. I have c ...

Encountering difficulty deserializing a JSONObject nested within a JSONArray in Java Spring

Here is the method I am working with: @RequestMapping(value = "/getFields", produces = "application/json") public Response getFields(@RequestParam(value = "id") int id) I have a subclass of Response that contains a JSONArray, which stores JSONObject inst ...

Question: When referencing $(this) in a different div, how can you navigate back to the previous div and access its children elements in jQuery?

Here's the code I've been working on: When a user clicks on a link with the class '.like', I want the total number of likes (found in the div.postInfo as '.PostlikeCount') to update and display the new total. $(".like").cl ...

I am looking to insert a jQuery value or variable into the plugin options

How can I insert a jQuery value or variable into plugin options? This is my current script: $(document).ready(function() { // var bannerheight = 580; if ($(window).width() < 2100) { var bannerheight = 410; var opts = JSON.parse( ...

"Clicking on the hamburger menu causes the webpage to reset its scroll

I recently integrated a mobile hamburger menu into my website, which is primarily built around a single page. However, I noticed that whenever I open the menu, it automatically scrolls to the top of the page regardless of where you were previously scrollin ...

What is the purpose of passing data into the 'success' function of an AJAX request?

Recently, I embarked on a journey to learn jQuery and AJAX, but I found myself tangled in confusion when it came to AJAX requests. To gain practice, I decided to create a simple TodoApp using Node, jQuery, and Bootstrap. While I managed to grasp GET and P ...

Arranging div containers with nested content

Within my application, I am dynamically displaying images side by side to users. Users have the ability to assign points to these images, and I would like to give them the option to sort the images based on points, with the highest points displayed first i ...