Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<String> in java to List in javascript

I have a java code that I want to change to javascript to add script in my html file. The fundamentals of my java code is to print out list of API names

public List<String> getApiNames() throws IOException{
    String url = "https:give me api / // ";

    HttpClient httpClient = new HttpClient(5,10000);

    Response response = httpClient.callGetRequest(url, null, null); 

    String webPage = response.getResponseBody();

    ObjectMapper mapper = new ObjectMapper(); 
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Map<String, Api> apis = mapper.readValue(webPage, new TypeReference<HashMap<String, Api>>(){});
    List<String> list = new ArrayList<String>(apis.keySet());
    return list;
}

and I want to add to my html that is formatted like this, list format

Is there anyway I can add the java code I have to html file?

like image 589
Sherly Kim Avatar asked Dec 10 '25 23:12

Sherly Kim


1 Answers

In JavaScript, List<> is an array. Example ["one", "two", "three"]. List sizes do not have to defined up front in javascript and are dynamic. Below is an example using jquery's ajax to call a URL and get data from it:

http://api.jquery.com/jquery.ajax/

function getAPIArray(callback) {
    var url = "https://someurl";
    $.ajax({
        type: 'GET',
        url: url,
        success: function (data) {
            //data will be the response from the server (the api's)
            // If it's not already in a list (array) then you have to build it up
            //Example:
            var array = [];
            for (var i = 0; i < data.results.length; i++) {
                array.push(data[i].apiName);
            }
            callback(array);
        },
        error: function (err) {
            alert('error!');
            console.log(err); //prints error object to console
        }

    });
}

function getsCalledWhenFinished(apiArray) {
    //do whatever you want with the data..
}

getAPIArray(getsCalledWhenFinished); //invokes the ajax call
like image 59
mwilson Avatar answered Dec 13 '25 12:12

mwilson