Bineet Kumar Nayak
Bineet Kumar Nayak

Reputation: 63

JSON string not returned properly in JSP

String json1 = new Gson().toJson(list);
System.out.println(json1);
request.getSession().setAttribute("json1", json1);

I checked the logs it prints string like : ["090856","056986"]

In JSP I wrote something line below in script

var n = '<%= session.getAttribute("json1") %>';
alert(n);

The alert displayed 090856,056986.

why such behaviour should not it display ["090856","056986"].

sometimes it also appears like this too

Alert Message

Upvotes: 1

Views: 89

Answers (2)

mirmdasif
mirmdasif

Reputation: 6354

There is nothing wrong in your your code. alert is modifying the string while showing.

  1. Use console.log(n) to test your json string.

  2. Or use JSON.stringify function

    alert(JSON.stringify(n));
    

Upvotes: 1

yurko
yurko

Reputation: 1562

It's normal behaviour.

You can try directly in chrome console.

  1. List item
  2. Go to chrome
  3. Press F12
  4. Go to Console tab
  5. Enter command : alert( ["090856","056986"]);
  6. As a result you have the following alert box

enter image description here

Upvotes: 2

Related Questions