Leveraging code behind and serialized JSON in an ASP.NET web application

I've recently created an array using a LINQ query:

var aTimeResultQuery = 
    (from fct in context.fct_testautomation
     join dr in context.dim_driver on fct.driver_key  equals dr.driver_key 
     join tc in context.dim_test_case on fct.test_case_key equals tc.test_case_key 
     join tr in context.dim_test_run on fct.test_run_key equals tr.test_run_key 
     where tr.test_suite_name == sSelectedTestSuite
     orderby fct.fct_testautomation_key descending
     select new                    
     {
          Duration = fct.Test_Duration,
          Target_Duration = fct.Test_Duration_Target_Max,
          Driver = dr.fused_driver,
          Test_Suite = tr.test_suite_name,
          Testcase = tc.test_case_type,
          Test_Description = fct.test_description
      })
      .Take(int.Parse(txtTestrun.Text)).ToArray();

After populating the object with this array in a for loop, I proceeded to serialize it:

JavaScriptSerializer aSerializer = new JavaScriptSerializer();

for (int i = 0; i < aTimeResultQuery.Count(); i++)
{
    aTimeGraph[i] = new TimeGraph();
    aTimeGraph[i].Duration = aTimeResultQuery[i].Duration.ToString();
    aTimeGraph[i].Target_Duration = aTimeResultQuery[i].Target_Duration.ToString();
    aTimeGraph[i].Driver = aTimeResultQuery[i].Driver.ToString();
    aTimeGraph[i].Test_Suite = aTimeResultQuery[i].Test_Suite.ToString();
    aTimeGraph[i].Testcase = aTimeResultQuery[i].Testcase.ToString();
    aTimeGraph[i].Test_Description = aTimeResultQuery[i].Test_Description.ToString();

    //Serializer.WriteObject(aMemoryStream, aTimeGraph[i]);
 }

 string sJson = aSerializer.Serialize(aTimeGraph);

The class structure for the object is as follows:

[DataContract]
public class TimeGraph
{
    [DataMember]
    public string Duration;
    [DataMember]
    public string Target_Duration;
    [DataMember]
    public string Driver;
    [DataMember]
    public string Test_Suite;
    [DataMember]
    public string Testcase;
    [DataMember]
    public string Test_Description;
}

Now, I'm facing a challenge on how to effectively use the JSON data in ASP.NET and iterate through it to utilize the attributes of the JSON object.

I have attempted to write the JSON string into a TextBox and retrieve the value like this:

var sResultString = document.getElementById('<%= TextBox1.ClientID %>').value;
var obj = $.parseJSON(resultstring);

I've tried to parse it using $.parsejson(jsonString) and JSON.parse(jsonString). While I am aware that this is not ideal, any help or suggestions would be greatly appreciated. Thank you!

Answer №1

let variable = eval($("[id$='InputBox1']").val());

Answer №2

Here is some JavaScript code you can use:

var sResultString = document.getElementById('<%= TextBox1.ClientID %>').value;
var obj = $.parseJSON(resultstring);
for(var i = 0; i < obj.length; i++){
  console.log(obj[i].Driver);
}

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

Asynchronous function nested within a loop

Hello there! I am currently working on converting a SQLite database to NeDb using the following code snippet: const sqliteJSON = require('sqlite-json'); const Datastore = require('nedb') const exporter = sqliteJSON('etecsa.db&apo ...

Exploring Angular unit testing using Jasmine: Techniques to customize or delete spyOn

Current software versions for AngularJS and Jasmine: AngularJS v1.2.26 Jasmine v2.2.0 I am facing an issue with a spyOn. When attempting to change or remove its behavior, I encounter the following error message: Error: getUpdate has already been spied u ...

Is Meteor.js the solution for time-triggered server requests?

We are currently in the process of developing an application that matches users from a database every Wednesday and Friday. How can we achieve this using Meteor? In the server code, I am considering organizing this functionality within a timedserver.js fi ...

Utilizing a JavaScript variable to fetch a rails URL: A comprehensive guide

One interesting feature I have is an image link that has a unique appearance: <a href="#user-image-modal" data-toggle="modal" data-id="<%= image.id %>"><img class="user-photo" src="<%= image.picture.medium.url %>" alt="" /></a&g ...

What is the best way to make img-fluid function properly within Bootstrap Carousel?

I've been struggling to make my carousel images responsive by using the img-fluid tag, but I haven't had any success. I've attempted using !important and display: block, but nothing seems to work. I'm not sure what's causing the is ...

Interacting with touch events in JavaScript on Android devices

I am facing an issue with my HTML page where a div is meant to function as an on-off switch momentarily. The functionality I have implemented looks like this: $('#btn').mousedown(function() { startAction() }) $('#btn ...

Single-page application powered by WordPress and universal JavaScript

Can a WordPress single-page application be created using isomorphic/universal JavaScript approaches (utilizing frameworks like React and Angular2)? ...

Quickly redesigning the appearance of file input using javascript and jquery. Seeking assistance to correct css issues on jsfiddle

Hey there, I've been working on styling the input[type="file"] element and could use some assistance. Like the saying goes, "One JSfiddle is worth 1000 words," so here's the link to my example: --- For more details, click here: http://jsfiddle.n ...

Looking for assistance with overriding kendo UI validation requirements

I'm looking to customize the date validation in my code to validate the date as DD/MM/YYYY. Here's my current code snippet and I'm getting an error message that says: Unable to get property 'methods' of undefined or null referen ...

What is the best way to automatically hide the Materialize CSS mobile navbar?

Recently, I completed a website called Link. Using only Materialize CSS, Vanilla JS, and plain CSS, I developed a single-page application that effectively hides and reveals different sections based on event listeners. Everything functions smoothly except ...

Initiating an audio call exclusively via SimpleWebRTC

I'm currently utilizing a jQuery plugin for WebRTC found here. My goal is to initiate an audio call only, but I am struggling to find a function within the plugin that allows for this. The code snippet I am using with autoRequestMedia set to false is ...

Extracting information from an NSDictionary

Within my NSDictionary, there is a diverse range of data. When it is logged, the output looks like this: [{"user_id":3016817,"grade":"A","percent":"93","grading_periods":[{"assignments":[{"points":100.0,"grade":"A","score":95.0,"percent":"93","comment":nu ...

To prevent the animation from overflowing, set the parent element to have hidden overflow while still displaying the child element

I am facing an issue with two menus that can be toggled using a switch. Each menu item has a dropdown that appears when clicked. The problem arises when switching between the menus, as there is an animation for the transition (slide in and slide out). I wa ...

What is the importance of accessing the session object prior to the saving and setting of a cookie by Express-Session?

Quick Summary: Why is it crucial to access the session object? app.use((req, res, next) => { req.session.init = "init"; next(); }); ...in the app.js file after implementing the session middleware for it to properly function? Neglecti ...

What is the process for capturing the entire REST API response in Vugen?

Having trouble obtaining the full JSON response in VUGEN. It's my first time scripting in VUGEN and I'm using the web-HTTP/HTML protocol. I've written a simple script to make a POST request to a REST service. Action() { web_rest("POST: htt ...

Having trouble with the AJAX request for retrieving image paths, parsing the JSON response into a JavaScript array, and attempting to render the images on the page

Struggling to implement a functionality that involves loading images from a PHP array into a JavaScript array using JSON messages and AJAX. The buildImage() function is used to display the first image in the array within the content div, with onclick event ...

Configuring environment variables during Jest execution

A variable is defined in my `main.ts` file like this: const mockMode = process.env.MOCK_MODE; When I create a test and set the variable to true, it doesn't reflect as `'true'` in the main file, but as `'false'` instead. describe ...

I am having trouble establishing a connection to the JavaScript MQTT server

Error Encountered: WebSocket Error 12031 - The Server Connection Was Reset In order to subscribe to MQTT messages from the user interface, the code below is being utilized. A Mosquitto broker is currently running on my local machine, with the IP address s ...

When the user hits the enter key, automatically submit a form without the need for

Is it possible to submit a form on enter using type="button"? Here are my input fields: <input type="text" id = "login-user" class="form-control log-input" placeholder="Username" required="required"> <input type="password" id="login-password" clas ...

Customizing Material UI Select for background and focus colors

I am looking to customize the appearance of the select component by changing the background color to "grey", as well as adjusting the label and border colors from blue to a different color when clicking on the select box. Can anyone assist me with this? B ...