0

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?

2 Answers 2

1

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

0

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

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.