Building a Java application that utilizes a JSON object

I am trying to convert a JSON format into Java code using JSONObject and JSONArray, but I am facing difficulties in getting the output in the desired format. The JSON format is provided below:

var transaction_Data = 
[
    {
    "key": "PASSED",
    "values": [

        {"x": "20 June", "y": 30},
        {"x": "21 June", "y": 50},
        {"x": "22 June", "y": 20},
        {"x": "23 June", "y": 60},
        {"x": "19 June", "y": 20},
        {"x": "24 June", "y": 10}
        ]
    },
    {
    "key": "FAILED",
    "values": [
            {"x": "19 June", "y": 50},
            {"x": "21 June", "y": 30},
            {"x": "20 June", "y": 20},
            {"x": "23 June", "y": 70},
            {"x": "22 June", "y": 45},
            {"x": "24 June", "y": 60}
     ]
   }
]

I would appreciate any guidance on how to create this json object in Java as I intend to use it for building a multibar graph using NVD3. Thank you in advance for your help!

Answer №1

Feel free to test it out using these Plain Old Java Objects.

class TransactionDetails {
    private String identifier;
    private List<DataInformation> dataList;
    public TransactionDetails(String identifier, List<DataInformation> dataList) {
        this.identifier = identifier;
        this.dataList = dataList;
    }
}
class DataInformation {
    private String name;
    private Integer value;
    public DataInformation(String name, Integer value) {
        this.name = name;
        this.value = value;
    }
}

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

Is there a way to link dynamic server fields with VueJS?

How can I bind data posted by the server with Vue.js in order to display the data instead of field names? <script> module.exports = { data: function () { return { filedNameFromServer: ['{{filed1}}' ...

Another drop-down is hiding the bootstrap-select drop-down from view

What could be causing some parts of the first drop-down menu to be hidden by another drop-down menu below in the code snippet provided? <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv= ...

Differences between user input in Java and Python

Once again, I find myself struggling to grasp the concepts of Java. After posing a question about Hello world in Java versus Python, I was advised to temporarily accept the language until I become more comfortable with it. However, this approach doesn&apos ...

The function URL.createObjectURL() is failing to work across all browsers

Despite my efforts, I'm feeling tired as none of the solutions seem to work for me. Here is the HTTP call in Angular that I am struggling with: $http({ method: 'GET', url: API_URL + 'v1/file/' + candidateId + '/download& ...

SyntaxError: Unexpected '<' symbol found in JavaScript file while attempting to import it into an HTML document

This issue is really frustrating me In my public directory, there is an index.html file Previously, I had a newRelic script embedded within the HTML in script tags which was functioning properly Recently, I moved the script to a separate JavaScript file ...

Is it necessary to store a file on your local device prior to uploading it to a MongoDB database?

Currently, I am in the process of learning how to upload images from my React website to my Mongo database using an express server. Most tutorials I have come across suggest saving the file locally on the express server before transferring it to the Mongo ...

Utilizing variables in GraphQL requests

UPDATE: see the working code below GraphiQL Query I have this query for retrieving a gatsby-image: query getImages($fileName: String) { landscape: file(relativePath: {eq: $fileName}) { childImageSharp { fluid(maxWidth: 1000) { base64 ...

Javascript doesn't seem to be executing properly after being echoed from PHP

I'm facing an issue where I am attempting to display some PHP code after a button click event in JavaScript, but the code is not executing and no errors are visible. <script type="text/javascript src="https://code.jquery.com/jquery- 3.4.1.min.js ...

Using JavaScript, transform a client's date/time string into a JSON-compatible date/time string

I need to find a way to convert a client's date/time string on a form into a JSON date/time string using JavaScript with moment.js for a Django REST API backend service. Here is the initial attempt: document.getElementById("dt_tm").value = moment(do ...

Creating static pages with dynamic nested routes is a powerful feature that can enhance the structure

I've been working on a website and I've encountered a problem. Essentially, I need to display movies by genre from a database. When a user clicks on a genre, it should take them to another list of movies in that genre. Then, when they click on a ...

Query to retrieve all IDs contained within the list stored in TinyDB

In my python project, I am utilizing TinyDB to enhance the logic. To improve efficiency, I need to be able to retrieve multiple objects in a single query, preferably using a list. listOfIDs = ['123', '456'] I am working with the lates ...

What is the best way to continuously call an asynchronous method in native JavaScript until a successful response is received?

There is a scenario where I have an asynchronous method that can either return success or failure. The requirement is to repeatedly call this async method from another function until it returns success. However, if it fails consecutively for 5 times, the ...

Ways to obtain an attribute through random selection

Figuring out how to retrieve the type attribute from the first input element: document.getElementById('button').addEventListener('click', function() { var type = document.querySelectorAll('input')[0].type; document.getE ...

Is it possible to bring in Node's path module by using the import statement like this: `import path from 'path'`?

My preference lies with using the import x from 'y' syntax, however, all that has been brought to my attention online is the usage of const path = require('path'). I am curious if there exists a method of importing the path module util ...

Connecting prop variables to component variables establishes a direct relationship between them

By assigning the props variable to a component variable, any changes made to the component variable will also reflect in the props... Parent Component: const prova = [ { 1: 'a' }, { 2: 'b' }, { ...

Javascript returning unexpected characters in image URL from ajax response

Within my Codeigniter web application, I am utilizing an ajax function to fetch data from the database in order to display it on the view. The data retrieved includes an image URL along with other fields. The issue arises when I receive the data within th ...

Having trouble retrieving object values from the request body during a POST request in [Node.js, MySQL]

I am currently developing a management system and working on creating POST requests for the API to add a new city to the database. However, I am facing an issue where instead of receiving the correct values from the request's body, I am getting "undef ...

Executing a function following the removal of an element

I am trying to remove an element after exiting a jQuery dialog. I have used the .remove() function, but the element is not accessible after executing .remove(). How can I "destroy" an object in JavaScript and allow it to be called again without refreshing ...

Are you experiencing problems with JSON formatting?

Currently, I am facing an issue with populating an HTML table using JSON data. The problem arises when I try to populate the table with the JSON data that was provided to me. After careful examination, I suspect that the issue might lie in the formatting o ...

Error 500 on Firebase: Issue solving "firebase" in "firebase.js" not resolved

Struggling to incorporate Firebase into my latest React project, I keep encountering the dreaded "The development server returned response error code: 500." Despite creating a firebase.js file to house my Firebase configuration details, I am at a loss as ...