Solved

javascript to java via JSON.

Posted on 2013-06-11
8
333 Views
Last Modified: 2013-11-29
Hi there;

I just try to have a 2 way connection between a simple java code and javascript code via JSON. Can someone provide me an example for this?

Regards.
0
Comment
Question by:jazzIIIlove
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
8 Comments
 
LVL 29

Assisted Solution

by:Randy Downs
Randy Downs earned 334 total points
ID: 39237853
Maybe this will help
http://stackoverflow.com/questions/3792315/javascript-to-java-using-json

On the client-side, if you support only modern browsers, you can use the native JSON.stringify() API. Otherwise, the json2.js library is fine.

On the server-side, there are a herd of libraries to have a look at:

Jackson
json-lib
gson
and many more Java JSON libraries...
I'm pointing you to Jackson first at it seems to be the fastest in many cases. However, I find its documentation harder to get my mind around every time I need to get back to it. Json-lib is sometimes easier to get to grasp with for smaller tasks that do not require top-speed, but with still completely acceptable results. Gson as also a good reputation and is very flexible, however the previous benchmarks I came across seemed to indicate that it did not perform as well as Jackson. The newly released 1.5 version might have improved that, but I don't know.

It comes down to the degree of flexibility you want, the performance you need, and whether you want a simple API or if you don't mind a more complex one.
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 166 total points
ID: 39237854
http://easyxdm.net/wp/
page with examples (check the third) : http://easyxdm.net/wp/category/examples/
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39259049
Hi;

I checked for Jackson but i couldn't find an example code.

Is it possible for all commenters to share a simple code here so that the answer will be valid even the links are gone?

Regards.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 82

Expert Comment

by:leakim971
ID: 39259171
Not sure you are talking to me because I provide a link to an example
But, yes, you need to read a little bit :
http://easyxdm.net/wp/2010/03/17/sending-and-receiving-messages/
0
 
LVL 29

Accepted Solution

by:
Randy Downs earned 334 total points
ID: 39259826
Here's the Jackson tutorial - http://wiki.fasterxml.com/JacksonInFiveMinutes

There are examples for each method.

JSON Three Ways

Jackson offers three alternative methods (one with two variants) for processing JSON:

Streaming API (aka "Incremental parsing/generation") reads and writes JSON content as discrete events.
org.codehaus.jackson.JsonParser reads, org.codehaus.jackson.JsonGenerator writes.
Inspired by the StAX API.

Tree Model provides a mutable in-memory tree representation of a JSON document.
org.codehaus.jackson.map.ObjectMapper can build trees; trees consist of JsonNode nodes.
The tree model is similar to the XML DOM.

Data Binding converts JSON to and from POJOs based either on property accessor conventions or annotations.
There are two variants: simple and full data binding
Simple data binding means converting to and from Java Maps, Lists, Strings, Numbers, Booleans and nulls
Full data binding means converting to and from any Java bean type (as well as "simple" types mentioned above)
org.codehaus.jackson.map.ObjectMapper performs the marshalling (writing JSON) and unmarshalling (reading JSON) for both variants.
Inspired by the annotation-based (code-first) variant of JAXB.
From usage perspective, one way to summarize these 3 methods is:

Streaming API is best performing (lowest overhead, fastest read/write; other 2 methods build on it)

Data Binding is often most convenient
Tree Model is most flexible
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39260998
Hi leakim971;

Great to see you in this question.

I was talking with Number-1 actually. I haven't seen the example coding but now above comment I have seen.

Cheers.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to dynamically set the form action using jQuery.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question