This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

JSON problem

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I am facing problem while passing json object

Following is the servlet code

JSONArray jsonArr=new JSONArray();

JSONObject json=new JSONObject();
json.put("title","Ramandeep");
json.put("start", new Date(2010,11,20));
json.put("end", new Date(2010,11,20));
json.put("allDay",true);
jsonArr.add(json);
request.addAttribute("jsonArr",jsonArr);


And Following is the jsp Code
<script type="text/javascript">

$(document).ready(function() {


var jsonObj=eval(<%=request.getAttribute("jArr").toString()%>);

});

</script>

but i am getting error

missing } after property list
[Break on this error] var jsonObj=eval([{"title":"Ramandee...end":Tue Dec 20 00:00:00 IST 3910}]);\n

If i remove date in jsonArray in servlet code then it works fine..
Can Anyone help me out Please

Thanks in Advance
Ramandeep S
 
Marshal
Posts: 67289
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things:

1) It might be helpful to point out what JSON library you are using. Making us guess isn't going to get you help fast.

2) Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.
 
RamandeepS Singh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Two things:

1) It might be helpful to point out what JSON library you are using. Making us guess isn't going to get you help fast.

2) Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.



Thanks for your reply first

I am using json 1.1
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

RamandeepS Singh wrote:
var jsonObj=eval(<%=request.getAttribute("jArr").toString()%>);

[...]

var jsonObj=eval([{"title":"Ramandee...end":Tue Dec 20 00:00:00 IST 3910}]);



You have to watch out for the escaping of the string.
 
RamandeepS Singh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hauke Ingmar Schmidt wrote:

RamandeepS Singh wrote:
var jsonObj=eval(<%=request.getAttribute("jArr").toString()%>);

[...]

var jsonObj=eval([{"title":"Ramandee...end":Tue Dec 20 00:00:00 IST 3910}]);



You have to watch out for the escaping of the string.



you mean to say : in the date???
but i need date object
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. I mean the argument you pass to eval. eval expects one string to evaluate. That is not what you supply.

Just put single quotes around the string created by your JSON library.

 
RamandeepS Singh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hauke Ingmar Schmidt wrote:No. I mean the argument you pass to eval. eval expects one string to evaluate. That is not what you supply.

Just put single quotes around the string created by your JSON library.

]

Thanks for replying

But it doesnot work
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ItDoesntWorkIsUseless
 
RamandeepS Singh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hauke Ingmar Schmidt wrote:ItDoesntWorkIsUseless



i am sorry..

I mean to say
still facing the same problem... i applied single quotes...
and still face the same problem.. when i remove dates in my servlet code then it runs..

Please help me out

Thanks
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I still don't know which Java/JSON library you use but the date (as seen in the error message) is not encoded properly. Does your JSON library understand how to handle dates? If it is the one from JSON.org then the list of supported data types is short: JSONObject javadoc.
 
RamandeepS Singh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hauke Ingmar Schmidt wrote:Ok. I still don't know which Java/JSON library you use but the date (as seen in the error message) is not encoded properly. Does your JSON library understand how to handle dates? If it is the one from JSON.org then the list of supported data types is short: JSONObject javadoc.



Hi Hauke Ingmar Schmidt
Thanks for replying

I am new to JSON i am using it first time
Jar file i put into my lib is Json-simple-1.1.jar

Thanks & Regards
Ramandeep S
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thats JSON-simple. Please see the mapping between types. You have to care for converting your date to a string yourself.
 
RamandeepS Singh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hauke Ingmar Schmidt wrote:Ok, thats JSON-simple. Please see the mapping between types. You have to care for converting your date to a string yourself.



ok. thanks a lot
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!