The foundation of JSON and its structural encoding

Looking to deserialize JSON from Java, here's how it's done:

Java

jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO);

JSON

  "accounts" : [ {
    "name" : "1009427721",
    "value" : 16850.79,
    "children" : [ {
      "name" : "BITCOIN EARNINGS",
      "value" : 10734.24,
      "children" : [ {
        "name" : "2017",
        "value" : 1037.82,
        "children" : [ {
          "name" : "07",
          "value" : 518.91
        } ]
      } ]
    },  ...

The deserialized Java POJO:

public class GraphDTO {

    private Set<Account> accounts = new HashSet<>();

    public Set<Account> getAccounts() {
        return accounts;
    }
}

Questions

  1. Is there a way to exclude "accounts" from the generated JSON (first line) ?
  2. When injecting the JSON form into JavaScript, it appears encoded like:
    var data =  { &#034;accounts&#034; : [ {
    ... How can this encoding be prevented ?

Answer №1

It may not be feasible to completely avoid dealing with the accounts, but there is a workaround:

jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts());

We eagerly await your solution for parsing JSON in javascript...

Answer №2

Here is how I managed to get it to work:

  1. Implementing the solution provided by User123:

    mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts())

  2. Passing the JSON data from Java to JavaScript (in a JSP) using:

    var jsonData = ${graphDTOJSON};

Big thanks for the help!

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

Encountered an issue when attempting to access a file on the desktop using Node.js

Here is the code I am using to read a simple file from my desktop: module.exports = function (app) { app.get('/aa', function(req, res) { fs = require('fs'); fs.readFile('‪C:\\Users\\t5678 ...

Fixed-positioned elements

I'm facing a small issue with HTML5 that I can't seem to figure out. Currently, I have a header image followed by a menu div containing a nav element directly below it. My goal is to make the menu div stay fixed when scrolling down while keeping ...

java.lang.RuntimeException: Activity ComponentInfo in Android could not be launched

Currently facing an issue while attempting to parse a JSON object Value on this website. I keep receiving a runtime error that states: Unable to start activity componentinfo: Illegal character in URL. Here is the code snippet: Attempting to address the &a ...

Form Input Field with Real-Time JavaScript Validation

Is there a way to validate the content of a textarea using pure JavaScript, without using jQuery? I need assistance with this issue. <script> function validate() { // 1. Only allow alphanumeric characters, dash(-), comma(,) and no spaces ...

Steps to close a socket upon session expiration

I am currently working on a small express application that also incorporates a socket program. Everything works perfectly when a user successfully logs in - it creates the session and socket connection seamlessly. However, I encountered an issue where eve ...

Incorporate another parameter into the current JSON data

I am currently facing the following scenario: Several servlets are setting the HttpServletResponse content-type to application/json. This is how I am outputting my JSON: out.write(new Gson().toJson(myObject)); where myObject is an object that structur ...

Decoding JSON (using $Ref in Angular)

Having an issue parsing my JSON data. In object 2, I have a "t_quartier" where the value is just a reference pointing to object 1. How can I retrieve this value when on item 2? https://i.sstatic.net/vVTWl.jpg Thank you very much. ...

Unable to produce audio from files

Attempting to incorporate sound files into my project using https://github.com/joshwcomeau/redux-sounds but encountering difficulties in getting it to function. Below is the code snippet I utilized for setup. Unsure if webpack is loading the files correctl ...

Stopping a Firefox addon with a button click: A step-by-step guide

Utilizing both the selection API and clipboard API, I have implemented a feature in my addon where data selected by the user is copied to the clipboard directly when a button is clicked (triggering the handleClick function). However, an issue arises when a ...

Creating a priority queue with reversed order in Java efficiently within O(n) complexity

Is there an efficient way to create a PriorityQueue in Java that organizes an unordered collection of numbers in reverse order in O(n) time? The constructors of PriorityQueue do not allow for both a collection and a comparator to define ordering. While y ...

Issue encountered in React: Unable to access object value within ComponentDidUpdate method

I'm struggling to retrieve the value from an object key. componentDidUpdate(prevProps) { if (prevProps !== this.props) { console.log("component did update in top menu", this.props.topmenudata[0]) this.setState({ ...

Reiterate list of inquiries using InquirerJS

How can the questions be reset or have a specific answer lead to another previous question? var questions = [{ { name: 'morefood', message: 'Do you want more food?', ...

Utilize PHP to group nested arrays retrieved from an SQL query and format them into JSON for use with the jq

Greetings to all on this platform! I am new here, so please bear with me if I have posted in the wrong section. Currently, I am working on a PHP page where I want to display data in a tree format using a jquery plugin named jqTree. The data is being fetch ...

Adjust the position of elements based on their individual size and current position

I am faced with a challenge regarding an element inside a DIV. Here is the current setup... <div id="parent"> <div id="child"></div> </div> Typically, in order to center the child within the parent while dynamically changing i ...

default selection in AngularJS select box determined by database ID

Here is the scenario: ... <select ng-model="customer" ng-options="c.name for c in customers"> <option value="">-- choose customer --</option> </select> .... In my controller: $scope.customers = [ {"id":4,"name":"aaaa", ...

Using AngularJS $http.jsonp() method to interface with Google Maps Distance Matrix API

I am currently working on integrating the Google Maps Distance Matrix API into my project to calculate distances between two points using specific coordinates. My implementation involves AngularJS and the $http.jsonp() method to make requests to the API: ...

Angular directive has issues with $compile functionality

This Angular directive automatically appends a new HTML item to the page every time my model changes: app.directive('helloWorld', function($compile) { return { restrict: 'AE', replace: true, scope:{ ...

AngularJS version 1.5.11 experiencing issues with ng-repeat functionality

Having an application built on angularJS v1.5.11, I encountered a major issue while attempting to use ng-repeat in a table format like below: <tbody> <tr ng-repeat="score in data.result"> <td ng-repeat="item in score"> {{ item }} & ...

Highcharts JavaScript - Sankey Graph Axis Orientation

As I work on developing a Sankey Diagram using the Highcharts JS library, I have encountered an issue with setting the axis. Can anyone advise whether it is feasible to utilize xAxis and yAxis in a Sankey diagram? I attempted to define the axis as shown b ...

Parsing poorly formatted data structures using JSON.NET

Due to reasons outside my control (the SurveyGizmo API), I must unpack data stored in the following (example) layout: // JSON data formatted by API [{ "id": "2", "contact_id": "", "status": "Deleted", "is_test_data": "1", "datesubmitte ...