tags:

views:

36

answers:

1

In my project I am using a JSON object. Here, they format the actual string into a JSON object. Wherever a colon occurs it will separate and print it in different lines in the jsp page, but in my case part of my data needs to display the colon in the jsp page. Because of the json object it will be print in separated line. For example, if my data is 1:a, I am getting output in jsp like

1
a

...but I need the output as 1:a. How can I do this?

A: 

If the value of the object is 1:a, then it should print out that way, assuming you are writing valid json - in this particular case, that would mean wrapping the value in double-quotes.

For example, your json could look something like this:

{"key1":"1:a", "key2":"2:b"}

In this case, the value for key1 would be 1:a and should display as such.

If, however, your json looks like this:

{1:a, 2:b}

you have invalid json, because your keys and values are not quoted. Aside from that, the colon is not going to print, because it is not a character, per se - it is an assignment operator. Expecting the colon to print would be analogous to assigning a variable to something, like:

var myVar = "the value";

and expecting to see

= the value

when you print it out.

If you need to print it as you suggest, I think your json should look like this:

var obj = {"1":"a", "2":"b"}

then probably do something like:

for (prop in obj) { document.write(prop + ':' + obj[prop] + '\n') };