Unable to retrieve Java variable in JavaScript

Seeking guidance on how to retrieve Json data stored in a Java variable using JavaScript. Instead of accessing the data, the html source is displayed.

Java:

 Gson gson = new Gson();
 String json = gson.toJson(obj);
 request.setAttribute("gsonData", gson);

JavaScript:

var dataset = ${gsonData};

The content in the html page source:

var dataset = {serializeNulls:falsefactories:[Factory[type=com.google.gson.JsonElement,
adapter=com.google.gson.internal.bind.TypeAdapters$25@719474],
com.google.gson.internal.bind.ObjectTypeAdapter$1@31d22,
... // (data continues)
instanceCreators:{}};

In what way can I appropriately access this data?

Answer №1

Your input was:

request.setAttribute("gsonData", gson);

However, consider changing it to:

request.setAttribute("jsonData", json);

So, should we replace the content of obj instead of using the GSon object?

Answer №2

Give it a shot using this code snippet:

let data = JSON.parse(jsonData);
data['skipNulls:truebuilders'].Builder;

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

unable to load variables in google extension due to technical difficulties

I'm encountering an error even though I've already loaded the DOM. I have no idea how to fix this issue and I've been sitting here for hours trying to troubleshoot. The error message I'm getting is: btncheck.js:10 Uncaught TypeError: C ...

Accessing attribute value of selected option in AngularJS select element

I have a select tag that displays options, and I want it so that when an option is selected, the value of the data-something attribute is copied into the input element. This is just for demonstration purposes; the actual value should be sent in the form. ...

Unable to invoke Kendo JavaScript

I'm facing an issue with my source code where I am trying to call kendo.all.min.js. Everything works fine when I call kendo.common.min.css and kendo.default.min.css, but as soon as I try to call kendo.all.min.js, I encounter the following error: http ...

Dealing with errors in AngularJS factory servicesTips for managing errors that occur

Factory code app.factory('abcFactory', function ($http, Config, $log) { var serviceURL = Config.baseURL + '/results'; return{ results:function() { var promise = $http({ method: 'GET&apos ...

A collection of text elements within an API request,

Correct Answer below. I need help with selecting products in bulk rather than individual calls. I've tried multiple syntax combinations but keep receiving 404 or 405 errors. I'm trying to compile a list of product codes, but the error codes per ...

Passing form data to PHP using AJAX in CodeIgniter Framework

I'm facing an issue with my HTML structure which is as follows: <form method="POST"> Name : <input id="asgname" type="text"> <br> Description : <input id="asgdescription" type="text"> <br> <a href="#" id=" ...

Utilize Gson to generate and analyze JSON data containing nested objects including a local date

I encountered an error while trying to parse my JSON data. java.lang.IllegalStateException: Expected a string but was NAME at line 1 column 313 path $.reminder com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was ...

Determining the size of the axis in correlation to a set constant length

Basic Concept I am currently developing a small tool designed to assist with geometric calculations for print-related items. Project Overview In this tool, users are asked to input the width and height of a box, represented visually as a CSS-based box, ...

Converting HTML widget code to NEXTjs code for integration in an application (CoinMarketCap Price Marquee Ticker)

Having trouble integrating the CoinMarketCap Price Marquee Ticker Widget into my NEXTjs app. I'm outlining my process and hoping for some suggestions from others who may have attempted this. Template Code: To embed the widget in an HTML page, here is ...

Vue.js does not receive the MQTT response message

I am a beginner with Vue and I'm currently working on a project where I need to set a default value for Vue data return(). Right now, when the code runs, it logs console.log('INSIDE CLIENT ON MESSAGE"). However, the value defined as this.roo ...

Utilizing Electron to save editable user data in a .txt file

I am making an electron app that converts data from .txt files to Javascript arrays. This data is stored inside a folder called faces in the main directory. I also have a button in my app which, when clicked opens file explorer at the faces folder so the u ...

Having trouble with a Pandas DataFrame - looking to filter by a JSON key that contains a date

I'm looking to filter this JSON data by date, but I'm struggling to figure out how. I've attempted different methods found online without success. Any help would be greatly appreciated. Below is my code snippet. import urllib.request import ...

The Material UI Drawer is fading into the background instead of smoothly sliding into view

Seeking guidance as a novice in SD, I am dedicating my free time to honing my skills. Any advice is appreciated, but please explain it as if I were a child. My current endeavor involves making the Drawer Component feature functional and customizing it to s ...

Unable to execute chrome driver in headless mode

System environment: JVM 11.0.6-open ubuntu 18.04 SDKMAN 5.8.1+484 Gradle 6.1 Chrome Version 81.0.4044.138 Chrome driver 3.141.59 Code to run: System.setProperty("webdriver.chrome.driver", "/snap/bin/chromium.chromedriver"); ChromeOptions options = new ...

Steps for converting a Calendar into a JSON format

I am working on developing a REST application like the one shown below: @XmlRootElement @Entity @Table(name = "tb_verarq") public class Verificacao implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue ...

Using Angular 2: A Beginner's Guide to Navigating with the Latest Angular 2.0.0-rc.1 Router

As I embarked on a new Angular 2 project, I was puzzled to discover that I inadvertently installed two different versions of the angular router: "@angular/router": "2.0.0-rc.1", "@angular/router-deprecated": "2.0.0-rc.1", Despite my best efforts, I co ...

Extract a JSON substring by referencing a specific object node key

I have a JSON string that contains nested objects. Here's a portion of the JSON string: "foldChildren": "false", "branchColor": "#000000", "children": [ ...

Disabling $routeprovider while implementing bootstrap

I am encountering an issue with my normal routeprovider code. In a specific section of my HTML, I have some Twitter Bootstrap expand/collapse sections which inadvertently trigger the routeprovider when clicked. Is there a way to prevent this from happening ...

Comparison Between MySQL and JSON - Which is the Better Choice?

I'm in the process of developing a new web application or game. When it comes to storing information, would it be more efficient to use MySQL tables or json files? Both options can hold data and can be processed using PHP. What are the benefits and dr ...

Immersive pop-up interface displaying a variety of embedded videos simultaneously

I am a beginner in JavaScript and I came across a CodePen that does exactly what I need, but it currently only works for one embedded video. What I aim to achieve is similar functionality, but with 6 videos. (function ($) { 'use strict'; ...