kartik
kartik

Reputation: 100

Java objects to JSON object

I am new to JSON , im trying to display the values fetched from DataBase in jsp using JSON , I was able to conver the java object to JSON object , but its failing to display the values in jsp . Can any one help me in this case ?

Code :

          <form name="viewform">

                 <%
                       List<ResourceMap> transList =(ArrayList)request.getAttribute("transaction");

                 GsonBuilder builder = new GsonBuilder();
                 builder.serializeNulls();
                 Gson gson = builder.create();
                 %>



                 <script>
                       var app = angular.module("myApp", []);
                       app.controller('myController', function($scope) {
                              $scope.jTranslist = gson.toJson(transList);
                       });
                 </script>

          </form>

transList is Java object (array list) and trying to convert to json .

Problem is data is not being displayed on jsp. What exactly is the problem ??

Upvotes: 0

Views: 107

Answers (1)

Shotgun Ninja
Shotgun Ninja

Reputation: 2540

The problem is that you're trying to expose the (server-side) Java object to the (client-side) Javascript code.

In order to do this successfully, you'd need some way of writing the JSON to the page. Instead of:

$scope.jTranslist = gson.toJson(transList);

Try using:

$scope.jTranslist = <%= gson.toJson(transList); %>;

However, I'm not positive this is even what you should be doing. Would AJAX be a better approach to loading JSON into the page for you? Also, wouldn't it be easier just to expose the raw list to the page and iterate over it via scriptlet or tag library?

Upvotes: 1

Related Questions