0
var  data = [
        {lastName: "Dente", name: "Al"},
        {lastName: "Noring", name: "Constance"},
        {lastName: "Haas", name: "Jack"},
        {lastName: "Tress", name: "Matt"},
        {lastName: "Turner", name: "Paige"}
    ];

this is the javascript array i want to create. How to send this data from a servlet and create the array object in javascript.

String dataString=/*prepare data */
response.getWriter().write(dataString);

How to prepare this dataString in servlet and how to create array from that string in the client ??

2 Answers 2

2

If you have the following Java class:

public class Person {
    private String lastName;
    private String name;

    public Person(String lastName, String name) {
        this.lastName = lastName;
        this.name = name;
    }

    /* getters and setters */
}

Then you can easily generate the data via the following using Gson:

List<Person> people = new ArrayList<Person>();

people.add(new Person("Smith", "John"));
people.add(new Person("Example", "An"));

String json = gson.toJson(people);

The json variable will then have a value of [{"lastName": "Smith", "name": "John"},{"lastName": "Example", "name": "An"}].

Then just send that as the response from your servlet, remember to set the content type of the request to application/json.

In order to get a handle on the data in JavaScript I would recommended using jQuery's .getJSON() method, e.g.

$.getJSON('url of servlet', function (data) {
    console.log(data); // <-- data will be an Array containing two objects
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your detailed explanation. after getting data from server, how can i create the array ?? this is what i actually need. is it correct to declare like this?? var data=dataStringFromServer;
If you use the code in my example the data variable in the callback function of the $.getJSON call with already be an Array. Are you currently getting the data back from the server in JSON format?
at present i am not using the jquery function. i am using the plain XMLHttp request. But i am getting the json string. So, i want to create the array object from that string. except that all i have done is same as you said.
OK, no problem. If you're using plain XHR you can just use var data = JSON.parse(str); where str is the data that was returned from your servlet. In order to ensure JSON.parse works in older browsers make sure you include the json2.js file in your page.
1

Use net.sf.json.JSONObject to achieve this in serverside

Serverside:

public class Data {
    String lastName;
    String name;
//......constructors,getters,setters
}

ArrayList<Data> serverData = new ArrayList<Data>();
serverData.add(new Data("Fred", "Flintstones"));
serverData.add(new Data("Wilma", "Flintstones"));
String jsonStr = JSONObject.fromObject(serverData);
response.getWriter().write(jsonStr);

Use JSON.parse in client side

Client side:

function ajaxCallback(data) {
    var jObj = JSON.parse(data);
    for(var i in jsonObject)
    {alert(jObj[i].name+" "+jObj[i].lastName);}
}

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.