Reputation: 4609
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?
Upvotes: 0
Views: 2417
Reputation: 4358
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
Upvotes: 0
Reputation: 268
You should use @Expose before each of your instance members in the class definition, then call the Gson file on that instance.
Upvotes: 1