Converting a Java Object array to a JavaScript Object Array when passing it as a

Is there a way to send a Java array of objects to a JavaScript function?

Here is an example object:

class SitioMapa{
    String title;
    double lat;
    double lng;
    String description;

    public SitioMapa(String title,double lat,double lng,String description) {
        this.title=title;
        this.lat=lat;
        this.lng=lng;
        this.description=description;       
    }
}

When converted to JSON, it looks like this:

"SitioMapa":[{"title":"Alibaug","lat":"18.641400","lng":"72.872200","description": "Alibaug is a coastal town and a municipal council in Raigad District in the Konkan region of Maharashtra, India."},{...},{...}]

I need to convert a SitioMapa[] array filled with objects like above to a format like:

var markers = [
        {
            "title": 'Alibaug',
            "lat": '18.641400',
            "lng": '72.872200',
            "description": 'Alibaug is a coastal town and a municipal council in Raigad District in the Konkan region of Maharashtra, India.'
        }
    ,
        {
            "title": 'Mumbai',
            "lat": '18.964700',
            "lng": '72.825800',
            "description": 'Mumbai formerly Bombay, is the capital city of the Indian state of Maharashtra.'
        }
    ,
        {
            "title": 'Pune',
            "lat": '18.523600',
            "lng": '73.847800',
            "description": 'Pune is the seventh largest metropolis in India, the second largest in the state of Maharashtra after Mumbai.'
        }
];

What is the best way to accomplish this task?

I have tried using JsonObject and JsonArray, but I am still unable to access the data in the array properly.

Answer №1

Find detailed instructions on Converting Java Objects To/From JSON using the Gson library by MKYong.

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 an improved method for toggling animations in CSS using jQuery compared to my current approach?

Looking to create a toggle effect for a dropdown box that appears and disappears when a button is clicked. var clickState = false; $("#show").on("click", function() { if (!clickState) { $(".animated").removeClass("off"); refreshElement($(".an ...

Understanding how the context of an Angular2 component interacts within a jQuery timepicker method

Scenario: I am developing a time picker component for Angular 2. I need to pass values from Angular 2 Components to the jQuery timepicker in order to set parameters like minTime and maxTime. Below is the code snippet: export class TimePicker{ @Input() ...

CSS struggles after implementing conditional checks in return statement for an unordered list

I'm encountering a CSS issue with the header section. When I include the following code snippet in my code to display a tab based on a condition, the entire header list doesn't appear in a single horizontal line: <div> {isAdmin(user) ? ...

What is the reason that an abstract class does not need to implement methods when inheriting from an

Currently, I am in the process of creating a class that will be implementing an interface. The interface itself contains only one method. What I find interesting is that, even after declaring my class to implement the interface and closing the brackets wit ...

What is the best way to navigate to a component that has been imported in Vue.js?

I have knowledge of Vue's scrollBehavior function, but I am struggling to integrate it with my existing code. On my Index page, I have sections composed of imported Vue components like this: <template> <div class="Container"> <Ab ...

Incorporating a fixed header title when creating a customizable table

I am working on creating a dynamic table with rows and columns based on JSON data. JSON: $scope.dataToShow=[ tableHeder=["First Name","Age"], { name:"rahim", age:23 }, ...

Unicef's animated web content

Looking to learn more about gate animation and zoom page transition on the Unicef website? I want to incorporate these cool animations into my own project. Can someone provide me with a "keyword" or point me in the right direction to find information on ...

Error in Android Studio: Issue converting org.json.JSONObject to JSONArray

I'm struggling to retrieve JSON data from an API that generates the user name and password. Any assistance would be greatly appreciated. ...

Ways to obtain user information using a Google access token

Hey there, I'm currently working on implementing Google OAuth2 login and I'm facing a challenge in retrieving user information using either the access token or id token from these URLs when passing the access token in the Authorization header, ...

gson: customized fromJson method based on the data type

Apologies if this has been asked before, but I couldn't find the information I'm looking for. Here are the different message types I have: class AbstractMessage { int code; String token; } class ShareMessage extends AbstractMessage{ ...

Utilizing jQuery for AJAX-Based Deletion of Records

Hey, I stumbled upon a code snippet that allows me to delete a row from my database using Ajax. However, there seems to be an issue where the table that should disappear when deleted is not working properly. Here is the JavaScript code: <script type=" ...

Leveraging jest for handling glob imports in files

My setup involves using webpack along with the webpack-import-glob-loader to import files utilizing a glob pattern. In one of my files (src/store/resources/module.ts), I have the following line: import '../../modules/resources/providers/**/*.resource. ...

Vue.js v-cloak lifecycle method

Currently, I am working on a project where I have styled v-cloak with display: none, and it is decorating the body. As a result, everything remains hidden until the Vue instance is ready. I have created a component that inserts a chart (using highcharts). ...

Guide to crafting an effective XHR request using AJAX

Here's a snippet of my basic web page: <!DOCTYPE html> <html> <head> </head> <body onload="start()"> </body> </html> Below is the XMLHttpRequest function I've implemented: function start(){ / ...

Filter out the truthy values in an array with JavaScript

I am currently working with a javascript array called 'foo' which looks like this: var foo = [false,false,true,false,true]; My goal is to eliminate all the 'true' values and only keep the 'false' ones, resulting in this arra ...

What section of the RAM do global variables, whether initialized or uninitialized, reside in?

Issue arises when defining arrays in the following manner: float Data_Set_X[15000]; float Data_Set_Y[15000]; float Data_Set_Z[15000]; An error occurs due to RAM overflow, specifically: .bss exceeding region RAM allocated for Timer-Blink-Test_CM7 C/C++ Pro ...

Transforming JSON data in a Node.js API using JSONata

In need of developing a Node.js REST API for transforming JSON to JSON format. After researching multiple libraries, I have narrowed down my options to "JSONata". You can find a simple sample of JSONata here. The main challenge lies in the fact that the ...

Implement a select element with an onchange event that triggers an AJAX

As a newcomer to Javascript, I've been struggling to find a solution to my issue. The goal is to add an additional select option when the previous select option is changed. I attempted to implement the code below, but it's not functioning correct ...

Implementing setInterval() leads to the dynamic alteration of images

I've created Marquees using CSS animations and a countdown timer. The Marquees display 100 random images that move from left to right and right to left. However, when the countdown timer decreases, the images in the Marquee change but the scrolling co ...

Tips for Loading a Selected Option from an Ajax Call in Angular JS on Page Load

How do I automatically set the selected option from an ajax call when the page loads? I am fetching zones using an ajax call and I want to trigger the change function of the zones on page load in order to set the selected option of the cities select box. ...