Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to send an array of pojo's as a response to an ajax call.

Inside of my pojo, I have the following toString():

@Override
public String toString() {
   return "Expense [period=" + period + ", description=" + description + 
               ", category="+ category + ", subCategory="+subCategory+", "
               + "amount="+amount+", store="+store+"]";
}

Then, inside of my doGet method, I build up the arraylist of pojos, and am trying to write them out, using:

    Gson gson = new Gson();
    String json = gson.toJson(expensesForPeriod);
    out.write(json);

Where expensesForPeriod is an arraylist of expense objects.

Is this the correct way to send an arraylist of json objects?

On the javascript side, how would I convert the json string to an array of objects, and iterate over them?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You should use @Expose before each of your instance members in the class definition, then call the Gson file on that instance.

share|improve this answer

For java side:

you shouldn't override toString method, just need to use Gson to parse object to json string

For javascript side:

you can follow this tutorial: link

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.