methods for obtaining access in JavaScript when dealing with an ArrayList<Object> that has been converted to a JSONArray

I am dealing with an Object ArrayList that contains various variables, stored in an ArrayList of Objects. I then convert it to a JSONArray and pass it to a JSP page. How can I display these objects in a textarea on the JSP page using JavaScript?

public class myObject{
    private String fileName;
    private String filePath;
    private List<String> messages;

    //Constructor, empty constructor and accessor methods.

}

In my Java Controller,

ArrayList<myObject> filesContainer = //obtained from a source file
JSONArray jsFileList = new JSONArray(fileSources);

ModelAndView mnv = new ModelAndView();
mnv.addObject("fileList", jsFileList);
mnv.setViewName("forward:Test.jsp");
return mnv;

In my JSP page, I have created a text area:

<textarea rows="10" cols="50" id="display"></textarea>

<script>
    function fileList(){
        <% JSONArray fileList;
        if(request.getAttribute("fileList") != null){
            fileList = (JSONArray) request.getAttribute("fileList"); 

            %>


        <% } %> 
    }
</script>

How would I go about accessing the fileName and filePath in my JSP file?

Answer №1

Give this a shot:

for( int i = 0 ; i < fileList.length() ; i++ ){
    JSONObject file = fileList.getJSONObject("test");
    System.out.println(file.getString("fileName"));
    System.out.println(file.getString("ms"));
}

Check out:

Answer №2

Here is the method I use to fetch the data. Much appreciated for sharing...

var x = "<%= j.getString("fileName") %>";

function retrieveFiles(){
        <% JSONArray fileList;
        if(request.getAttribute("fileList") != null){
            fileList = (JSONArray) request.getAttribute("fileList");%>

            <%
                for(int i=0;i<fileList.length();i++){ %>

                <% JSONObject j = fileList.getJSONObject(i); %>
                var x = "<%= j.getString("fileName") %>";
                document.getElementById("fileSelector").value += x + "\n";

            <% } %>


        <% } %> 
    }

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

Exploring the world of Jquery, JSON, and interactive

Seeking assistance as a newcomer to JQuery in structuring my code... I have managed to create a JSON page and display the desired results. Now, I am looking to organize these outcomes into tabs. Each tab should contain the title of an article with the body ...

Tips for paginating a Laravel collection without using keys:

My task involves paginating an array of values with keys (e.g., 0,1,2) and extracting the values without their keys. Initially, the array looks like this: array:11 [ 0 => {#829 +"id": 2417 +"date": "2019-12-24 15:05:04" +"eventName": "At ...

When the mouse leaves, the gauge chart component's size will expand

I have encountered a problem while using the react-gauge-chart library in my react project. Within the project, I have integrated a popover component using material-ui and incorporated the gauge chart component from the library within the popover modal. T ...

Exploring JSON data structures using autocomplete functionalities

Here's the code I'm working with: <s:hidden id="s" value="%{Users}"/> The variable Users contains an array list of User objects. This code is written in Javascript. I want to access Users as JSON for auto-complete functionality: var valu ...

"Utilizing JSON information to create visually appealing graphs and charts

Struggling with syntax and in need of assistance. I have a basic data set that I want to display as a timeline with two filled lines (Time Series with Rangeslider). This is the format of my data set: [{"pm10": 12.1, "pm25": 7.0, "time": "13.08.2018 12:25 ...

Troubles encountered while processing STL file readers

Utilizing STLLoader.js, I am able to render components successfully, however, they all appear with one uniform color which does not resemble real-world components. The image above showcases my implementation in Three.js using STLLoader.js (Binary STL file ...

How can I set up mixare with JSON data?

$query = "SELECT id, latitude, longitude, elevation, title, distance, has_detail_webpage, webpage, info FROM korban"; $q=mysqli_query($connection, $query); // Assume $connection is the MySQL connection variable //echo $query; while($row=mysqli_fetch_assoc( ...

Trouble with transmitting props to function

The child component is not receiving the props as expected. When printed, it displays an empty object. The problem seems to be in the News.js file specifically related to passing props to the child component from App.js. All functions are operational excep ...

The JSColor onChange event is throwing an error indicating that the function is not defined

When attempting to use the onChange event for JSColor to call a function, I consistently encounter an error indicating that the function is not defined. The code snippet below illustrates the issue: export class NavBar extends React.Component { constr ...

What is the best way to enable ondemand icon usage in Vue Vite 3 with Iconify?

Did you know in the iconify documentation, there is just one simple step to follow? Visit All you have to do is: npm install --save-dev @iconify/vue import { Icon } from '@iconify/vue'; <Icon icon="mdi-light:home" /> After thi ...

Attempting to rename the "like" button in Django Ajax, however encountering difficulties

My button is currently functioning, but it's embedded within a Django for loop. I want to move the JavaScript logic to a separate file, but before that, I need to rename it appropriately. Check out this excerpt from my code: {% for post in posts %} ...

Click on the button to fetch data from the database using AJAX in Laravel

My goal is to dynamically load the `data-id` from a drop-down menu when a list item is clicked, retrieving the data from a database and appending it to a table. Currently, I have successfully loaded the data id in the drop-down menu and upon clicking on a ...

Receiving JSON in a WCF DataService: Step-by-Step Guide

Currently, I am exploring how to utilize WCF Data Services (built on EF 4.1) to establish a restful web service that would store entities passed as JSON objects. I have managed to develop a method capable of receiving a GET request with a collection of pr ...

Guide to establishing a connection to the Companies House API: Essential guidelines on setting up cURL headers and requisite API key specifications

I am attempting to establish a connection with the UK Companies House API, preferably using JavaScript. However, I am currently trying to set up this PHP version. How can I obtain access to the API using an API key? PHP: public function GetCompanyHouse( ...

How to optimize updating object properties with setState?

Is there a more efficient way to rewrite this code? For example, would using setState(ContentStore.getAll()) achieve the same outcome? I believe this approach appears overly cluttered and any method to simplify the code for readability would be greatly app ...

What is the correct method for iterating through this array of objects and eliminating those with either null or empty item.text values?

Currently, I am working with the following Array: const contactInfo = computed(() => { return [ { id: 'address', icon: 'detail/clubLocation', text: `${props.data.contactInfo.address}` ...

Node.js refusing to acknowledge the get request handler for the homepage of the website

My Node.js server setup is quite simple: const express = require('express'); const app = express(); const http = require("http"); const path = require('path'); const favicon = require('serve-favicon'); // Public fil ...

Tips for enhancing the functionality of components within Vue

My expertise lies primarily in React, and I'm now exploring the Vue-centric approach to achieve the following: I want to enhance this component: , so that the label-position is set to top on mobile devices and left on desktop. However, I'm not s ...

A more efficient approach to utilizing the json mixin feature with Spring 4 controllers

Is there a more efficient approach (perhaps utilizing annotations) to incorporate a mixin into a spring controller? At present, my method involves: @RequestMapping(value = "/accounts", produces = MediaType.APPLICATION_JSON_VALUE, method = ...

What is the reason for the lack of arguments being passed to this Express middleware function?

I have been developing a middleware that requires the use of `bodyParser` to function, however I do not want to directly incorporate it as a dependency in my application. Instead, I aim to create a package that includes this requirement and exports a middl ...