0

I have this in my console chrome: [{"id":40,"endDate":"2017-04-22","dataA":"2017-04-19","nrC":2,"type":"CO","dataD":"2017-04-19","startDate":"2017-04-20"},{"id":40,"endDate":"2017-04-26","dataA":"2017-04-26","nrC":4,"tyoe":"CP","dataD":"2017-04-23","startDate":"2017-04-25"},

This json string is comming from the servlet that calls DAO class to take the information from db. So, this string is (dynamically) passed into a jsp page from request session...and i put it into var DAYS = '${jsonArrayString}'; then console.log(DAYS); then it prints that json string above. So...it can print more data then two. It has to be put into a javascript variable loke this:

var DAYS = '${jsonArrayString}'; //That's what's on my console..and it comes from session into this jsp page

I think it has to be iterated through a foreach and print it in that format.

var USER_DAYS   =   [  

                  {
                      id: value from jsonArrayString,
                      date: value from jsonArrayString,
                      title: value from jsonArrayString,
                      start: new Date(value from jsonArrayString),
                      end: new Date(value from jsonArrayString),
                      allDay: true,
                      className: 'done'
                  },            
                ];

I tried to put the values ​​manually and it works...like this:

var USER_DAYS   =   [
      {
          id: 1,
          date: '2017-04-05',
          title: 'CO',
          start: new Date(2017, 3, 5),
          end: new Date(2017, 3, 7),
          allDay: true,
          className: 'done'
      },

I don't know hot to put the values from that json string( Which can be anythong ... more than 2 records)...that why I need to iterate through that json string. I want the values to be put only in that format, in that variable (var USER_DAYS)

I tried somthing like this, but it does't work:

<c:forEach items="${jsonArrayString}" var="jsonArrayString">
                    {
                        id: '${jsonArrayString.nrC}' ,
                        date: '${jsonArrayString.dataD}' ,
                        title: '${jsonArrayString.type}' ,
                        startDate: '${jsonArrayString.startDate}',
                        endDate: '${jsonArrayString.endDate}',
                        allDay: true,
                        className: 'done'
                    },
</c:forEach> 
                ];

or like this:

var USER_DAYS   =   [  

                  {
                      id: DAYS.nrC,
                      date: DAYS.dataD,
                      title: DAYS.type,
                      start: new Date(DAYS.startDate),
                      end: new Date(DAYS.endDate),
                      allDay: true,
                      className: 'done'
                  },            
             ];

How to do this?

Taylor
  • 33
  • 3
  • 8

2 Answers2

0

try parse to json string into json objects.

Example

   var USER_DAYS = JSON.parse('${jsonArrayString}') 
0

JavaScript code runs only on the client side, while JSP on the server. You cannot iterate over the JavaScript variable while the page is rendered at the server.

  • If you want to serve a page that already contains the data, you should pass the data to the JSP page as a model attribute. To do this, take a look at this post.
  • If you want to populate the page with data after it has been loaded to the browser, you have to use a JavaScript library, such as jQuery. In this case, you have to send a request to the server to get the data in JSON format, and then you can manipulate them on the client side.
Community
  • 1
  • 1